System.Reflection no methods
I'm trying to enumerate all methods in an assembly and add them to nodes in a treeview:
private void bOpen_Click(object sender, EventArgs e)
{
var ofd = new OpenFileDialog();
if (ofd.ShowDialog() != DialogResult.OK)
return;
var asm = Assembly.LoadFile(ofd.FileName);
foreach (Module module in asm.GetModules())
{
var tnode = new TreeNode(module.Name);
for开发者_C百科each (MethodInfo method in module.GetMethods())
{
tnode.Nodes.Add(method.Name);
}
treeView1.Nodes.Add(tnode);
}
}
The problem is that no methods come under any the modules. I know it's nothing to do with treeview since module.GetMethods().Length returns 0. Is there something I'm missing?
You're looking for the methods in the modules in the assembly, rather than in the types in the assembly. Change your loop to:
foreach (Type type in asm.GetTypes())
{
var tnode = new TreeNode(type.Name);
foreach (MethodInfo method in type.GetMethods())
{
tnode.Nodes.Add(method.Name);
}
treeView1.Nodes.Add(tnode);
}
精彩评论