如何检索* .DLL所有公共方法方法、DLL

2023-09-03 02:40:55 作者:那台词、经典

我的* .dll用C#,我需要得到包含在* .dll文件的所有公共方法或类列表。是否有某种方式与C#编程做呢?

I have *.dll written with C# and I need to get list of all public methods or classes contained in that *.dll. Is there some way to do it programmatically with C#?

推荐答案

是使用Assembly.GetTypes提取所有类型,然后使用反射在每个类型重复的公共方法。

Yes use Assembly.GetTypes to extract all of the types, and then use reflection on each type to iterate the public methods.

Assembly a = Assembly.LoadWithPartialName("...");
Type[] types = a.GetTypes();
foreach (Type type in types)
{
    if (!type.IsPublic)
    {
        continue;
    }

    MemberInfo[] members = type.GetMembers(BindingFlags.Public
                                          |BindingFlags.Instance
                                          |BindingFlags.InvokeMethod);
    foreach (MemberInfo member in members)
    {
        Console.WriteLine(type.Name+"."+member.Name);
    }
}