Is it possible to show all methods and their access modifiers?

痴心易碎 提交于 2019-12-10 22:17:35

问题


I am doing a code review on some large class libraries and I was wondering if anyone knows of an easy easy way to generate a list of all the methods (and possibly properties/variables too) and their access modifiers. For example, I would like something like this:

private MyClass.Method1()
internal MyClass.Method2()
public MyOtherClass.Method1()

Something kind of like a C++ header file, but for C#. This would put everything in one place for quick review, then we can investigate whether some methods really need to be marked as internal/public.


回答1:


Yup, use reflection:

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

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




回答2:


Well, you can certainly use reflection for this, to enumerate the methods.




回答3:


Exuberant ctags has a mode for C# and is easy to use. That said, I would just reflect the assembly.




回答4:


There are tools you can use, like Reflector




回答5:


.Net Reflector or ildasm.

ildasm will generate a nice file for you if you ask it to export but only ask for the specific members you require.

NDepends will also do it (and with greater flexibility) but, for comercial use, costs money.




回答6:


If you're using Visual Studio, then you can always get that view.

Just use the "Go to Definition" option and Visual Studio opens up the type metadata in a new tab (if you're using the DLL, and not the source code directly).



来源:https://stackoverflow.com/questions/532588/is-it-possible-to-show-all-methods-and-their-access-modifiers

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