且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否可以显示所有方法及其访问修饰符?

更新时间:2023-11-10 19:21:22

是的,请使用反射:

foreach (Type type in assembly.GetTypes())
{
    foreach (MethodInfo method in type.GetMethods(BindingFlags.Public |
        BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance))
    {
        Console.WriteLine("{0} {1}{2}.{3}", GetFriendlyAccess(method),
            method.IsStatic ? "static " : "", type.Name, method.Name);
    }
}

我将把GetFriendlyAccessName留给读者作为练习-使用IsFamily,IsPrivate,IsPublic,IsProtected等-或Attributes属性.

I'll leave GetFriendlyAccessName as an exercise to the reader - use IsFamily, IsPrivate, IsPublic, IsProtected etc - or the Attributes property.