如何获取特定dll中的所有类,浏览OpenFileDialog

时间:2014-10-10 11:08:31

标签: c# winforms reflection

我一直在尝试编写一个代码来获取特定dll运行时的所有类。用户必须使用OpenFileDialog选择Dll。选择后,它会列出所选dll中的所有类。我尝试过以下代码但没有成功。还谷歌,但没有找到任何具体的解决方案。

 private void button1_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
           Type t = openFileDialog1.OpenFile().GetType();
        }           
    }

1 个答案:

答案 0 :(得分:0)

试试这个:

var assembly = Assembly.LoadFrom(openFileDialog1.FileName);

foreach (var type in assembly.GetExportedTypes())
{
   Console.WriteLine(type.Name);
}

它使用给定名称加载程序集,获取所有类型并列出它们。