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

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

本文介绍了是否可以显示所有方法及其访问修饰符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对一些大型类库进行代码审查,我想知道是否有人知道一种简便的方法来生成所有方法(可能还有属性/变量)及其访问修饰符的列表.例如,我想要这样的东西:

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()

有点像C ++头文件,但适用于C#.这样可以将所有内容放在一起进行快速审查,然后我们可以调查是否确实需要将某些方法标记为内部/公共方法.

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.

推荐答案

是的,请使用反射:

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.

这篇关于是否可以显示所有方法及其访问修饰符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 05:53