开发者

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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜