简体   繁体   English

System.Reflection 没有方法

[英]System.Reflection no methods

I'm trying to enumerate all methods in an assembly and add them to nodes in a treeview:我正在尝试枚举程序集中的所有方法并将它们添加到 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);
            foreach (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?我知道这与 treeview 无关,因为 module.GetMethods().Length 返回 0。我缺少什么吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM