Posts Tagged ‘ASSEMBLY’
Listing All the references of Assembly
We can list all the references of the assembly in a recursive manner. Here i have avoided System Assemblies since those are not required & they are in circular references. Download the code from this link
private void btnList_Click(object sender, EventArgs e)
{
txtReferences.Text = "";
OpenFileDialog openFileDlg = new OpenFileDialog();
if (openFileDlg.ShowDialog() == DialogResult.OK)
{
txtAssemblyPath.Text = openFileDlg.FileName;
Assembly assembly = Assembly.LoadFrom(txtAssemblyPath.Text);
ListReferences("",assembly);
}
MessageBox.Show("Listing Completed");
}
public void ListReferences(string location, Assembly assembly)
{
//List all references
string name = assembly.GetName().Name;
string CurrentLocation = location;
txtReferences.Text += CurrentLocation + ">" + name + "\r\n";
location += "--";
if (!name.StartsWith("System.") && !Name.Equals("System"))
{
foreach (AssemblyName assemblyName in assembly.GetReferencedAssemblies())
{
assembly = Assembly.Load(assemblyName);
ListReferences(location, assembly);
Application.DoEvents();
}
}
}
Listing Types and Methods of assembly, Reflection
Some time there can be requirment in which we need to list the types and methods of assembly. For that purpose we have to use the reflection with help of that we can get all the details of the assembly even the IL code also. one samle to list the Types, associated methods and there properties is given below
//Get the assembly object
Assembly assembly = Assembly.LoadFrom(openFileDlg.FileName);
//List them all
foreach (Type type in assembly.GetTypes())
{
//Create a node of types first
TreeNodetreeNode = assemblyTreeView.Nodes.Add(type.Name);
foreach (MethodInfo mthdInfo in type.GetMethods())
{
//Get the node as method name
TreeNodemethodNode = treeNode.Nodes.Add(mthdInfo.Name);
//List the details of the method as child node
methodNode.Nodes.Add("Return Type : " +
mthdInfo.ReturnType.ToString());
methodNode.Nodes.Add("IsAbstract : " +
mthdInfo.IsAbstract.ToString());
methodNode.Nodes.Add("Access Modifier : " +
(mthdInfo.IsPublic ? "Public" : "Private"));
methodNode.Nodes.Add("IsStatic : " +
mthdInfo.IsStatic.ToString());
}
}
It is not limited to this much even you can create a type out of it and even can invoke methods of those type. This can be very useful when you want to create instance of class at runtime after locating in the various assemblies