How to retrieve all public methods from *.dll

旧巷老猫 提交于 2019-11-27 15:04:40

问题


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#?


回答1:


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



回答2:


var assembly = // grab assembly
var types = assembly.GetExportedTypes();

foreach (var type in types) {
    var methods = type.GetMethods(BindingFlags.Public);
}

GetExportedTypes will return all public types in the assembly. You also didn't specify whether you wanted just instance methods, static methods or both.




回答3:


Use System.Net.Reflection. Reflection classes let you query the metadata of types of a DLL at run time.

Something like this.GetType().Assembly.GetTypes();



来源:https://stackoverflow.com/questions/7454938/how-to-retrieve-all-public-methods-from-dll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!