C# - >用于查看代码的代码

时间:2012-02-10 15:47:49

标签: c#

是否有一种很好的方法可以打印项目或文件中所有方法的列表?

从那里,类或变量列表等。

6 个答案:

答案 0 :(得分:2)

取决于你想要做什么。如果您要分析.NET可执行文件,可以使用其中一种免费的反编译工具(dotPeekILSpy甚至.NET Reflector)。

但是,如果您在运行时查找此信息,也就是说,您希望打印在应用程序中执行的所有类型和方法,则可以使用Reflection(这里是CodeProject上的good tutorial )或使用AOP解决方案(如PostSharp)打印所有正在执行的方法的名称(参见Tracing example)。

答案 1 :(得分:2)

您正在寻找System.Reflection命名空间。您必须使用BindingFlags枚举来获得您想要的内容,但这是一个快速示例程序:

using System;
using System.Reflection;

namespace ReflectionExample
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Type[] types = typeof (MyClass).Assembly.GetTypes();

            foreach (Type type in types)
            {
                Console.WriteLine(type.Name);

                MemberInfo[] members = type.GetMembers(
                    BindingFlags.Instance | BindingFlags.Public
                    | BindingFlags.FlattenHierarchy | BindingFlags.DeclaredOnly);

                foreach (MemberInfo memberInfo in members)
                {
                    Console.WriteLine("\t" + memberInfo.Name);
                }
            }

            Console.ReadLine();
        }

        public class MyClass
        {
            public int Bar { get; set; }

            public void AMethod()
            {
                Console.WriteLine("foo");
            }

            public void BMethod()
            {
                Console.WriteLine("foo");
            }

            public void CMethod()
            {
                Console.WriteLine("foo");
            }
        }
    }
}

或者,您可以///记录项目中的所有内容,然后在项目设置中打开XML文档输出。我想这取决于你需要这个列表的内容。

答案 2 :(得分:1)

您可以使用Reflection列出类,方法和属性。

下面 Reflection

希望这有帮助。

答案 3 :(得分:0)

我不确定打印功能,但你看过.Net Reflector吗?

答案 4 :(得分:0)

通过反射,您可以找到有关函数,类等的任何元数据。 C#对它有很好的支持。

答案 5 :(得分:0)

        string dllPathName = @"dllPath\dllFileName";
        string outputFilePathName = @"outputPath\outputFileName";
        System.Reflection.Assembly fileNetAssembly = System.Reflection.Assembly.LoadFrom(dllPathName);
        StringBuilder sb = new StringBuilder();

        foreach (Type cls in fileNetAssembly.GetTypes())
        {
            //only printing non interfaces that are abstract, public or sealed
            sb.Append(!cls.IsInterface ? (cls.IsAbstract ? string.Format("Class: {0} is Abstract.{1}", cls.Name, System.Environment.NewLine) :
                                            cls.IsPublic ? string.Format("Class: {0} is Public.{1}", cls.Name, System.Environment.NewLine) :
                                            cls.IsSealed ? string.Format("Class: {0} is Sealed.{1}", cls.Name, System.Environment.NewLine) :
                                            string.Empty) : 
                                        string.Empty);

            if (!cls.IsInterface && (cls.IsAbstract || cls.IsPublic || cls.IsSealed))
            {
                //printing all methods within the class
                foreach (System.Reflection.MemberInfo method in cls.GetMethods())
                {
                    sb.Append("\t- ");
                    sb.Append(method.Name);
                    sb.Append(System.Environment.NewLine);
                }
            }
        }

        //output the file
        File.WriteAllText(outputFilePathName, sb.ToString());
相关问题