using System.Reflection;
private void Form1_Load(object sender, EventArgs e) {
Type t = typeof(System.Windows.Forms.Form);
MethodInfo[] methods = t.GetMethods();
foreach (MethodInfo nextMethod in methods) {
treeView1.Nodes[0].Nodes.Add(nextMethod.ToString());
}
PropertyInfo[] properties = t.GetProperties();
foreach (PropertyInfo nextProperty in properties)
{
treeView1.Nodes[1].Nodes.Add(nextProperty.ToString());
}
EventInfo[] events = t.GetEvents();
foreach (EventInfo nextEvent in events)
{
treeView1.Nodes[2].Nodes.Add(nextEvent.ToString());
}
}
III. Inspecting AssemblyInfo Attributes
The AssemblyInfo class contains additional information about an Assembly like Assembly Description, Copyright Information, Culture, etc. You can view the AssemblyInfo file by going Solution Explorer and selecting Show All Files icon and navigating to Properties node.
Here is an extract of the contents of AssemblyInfo.
AssemblyInfo.cs
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
[assembly: AssemblyTitle("Shalvin's Assembly Inspector")]
[assembly: AssemblyDescription("Shalvin")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Shalvin Consulting")]
[assembly: AssemblyProduct("WindowsApplication1")]
[assembly: AssemblyCopyright("Copyright © shalvin@gmail.com")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("afa9b674-416d-4e04-bf6a-805a99e98fe5")]
Here is the code to extract the Assembly Description.
using System.Reflection;
private void Form1_Load(object sender, EventArgs e)
{
Assembly a = Assembly.GetExecutingAssembly();
Type attrType = typeof(AssemblyDescriptionAttribute);
object[] attrs = a.GetCustomAttributes(attrType, false);
if (attrs.Length > 0)
{
AssemblyDescriptionAttribute desc = (AssemblyDescriptionAttribute)attrs[0];
listBox1.Items.Add(desc.Description);
}
Type attrComp = typeof(AssemblyCompanyAttribute);
object[] atCo = a.GetCustomAttributes(attrComp, false);
if (atCo.Length > 0)
{
AssemblyCompanyAttribute aca = (AssemblyCompanyAttribute)atCo[0];
listBox1.Items.Add(aca.Company);
}
}
Object Browser in C#
using System.Reflection;
List<Type> lsTypes = new List<Type>();
private void Form1_Load(object sender, EventArgs e)
{
Assembly LoadAssembly = Assembly.Load("mscorlib.dll");
Type[] mytypes = LoadAssembly.GetTypes();
int i = 0;
foreach (Type types in mytypes)
{
treeView1.Nodes.Add(types.ToString());
MethodInfo[] mi = types.GetMethods();
treeView1.Nodes[i].Nodes.Add("Method");
foreach (MethodInfo m in mi)
{
treeView1.Nodes[i].Nodes[0].Nodes.Add(m.ToString());
}
PropertyInfo [] pi = types.GetProperties();
treeView1.Nodes[i].Nodes.Add("Properties");
foreach (PropertyInfo p in pi)
{
treeView1.Nodes[i].Nodes[1].Nodes.Add(p.ToString());
}
EventInfo[] ei = types.GetEvents();
treeView1.Nodes[i].Nodes.Add("Events");
foreach (EventInfo e1 in ei)
{
treeView1.Nodes[i].Nodes[2].Nodes.Add(e1.ToString());
}
i++;
}
No comments:
Post a Comment