从类中提取属性,字段和方法名称的列表

时间:2015-08-13 19:21:39

标签: c#

让我们说Project是包含我的C#解决方案的所有类和相关文件的目录。

现在我想获得这些类中使用的所有属性,字段和方法名称的列表。我怎样才能做到这一点?我的第一个猜测是使用正则表达式然后我虽然它可能非常容易出错。然后我看到了this,但我不知道它是否适合我的情况,我也不知道如何使用它。

那么有一个程序可以做到吗?

4 个答案:

答案 0 :(得分:2)

如果您还没有所有文件,请创建一个包含所有文件的解决方案,并使用反射迭代类型和成员。

foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
{
    foreach (var prop in type.GetProperties())
    {
        //Iterating properties
    }

    foreach (var prop in type.GetFields())
    {
        //Iterating fields
    }

    foreach (var prop in type.GetMethods())
    {
        //Iterating methods
    }

    //Or iterate through all members

    foreach (var member in type.GetMembers())
    {
        //Iterating properties
    }
}

答案 1 :(得分:1)

反射:

        foreach (Type thisType in Assembly.GetExecutingAssembly().GetTypes())
        {
            foreach(PropertyInfo thePropertyInfo in thisType.GetProperties())
            {
                //Do something with it
            }
            foreach(MethodInfo theMethodInfo in thisType.GetMethods())
            {
                //Do something with it
            }
        }

确保添加Using System.Reflection

答案 2 :(得分:1)

你可以像其他人说的那样迭代ExecutingAssembly。 如果您只想使用此名称,请使用此呼叫

static void Main()
{
    CProjectReflector reflector = new CProjectReflector();
    Console.WriteLine(reflector.ToString());
    Console.ReadLine();
}

你必须创建两个类。 在CProjectReflector类中,您必须将其添加到文件的顶部

using System.Reflection;

如果您想了解更多反映,可以访问MSDN

通过反思你可以做更多的事情,它真的值得一读。 简而言之就是这个

  

Reflection提供描述程序集的对象(类型为Type),   模块和类型。

<强> CProjectReflector.cs

  class CProjectReflector
    {
        private List<CProjectObject> _projectObjects;

        public List<CProjectObject> ProjectObjects 
        {
            get { return _projectObjects; }
            set { _projectObjects = value; }
        }

        public CProjectReflector()
        {
            _projectObjects = new List<CProjectObject>();

            foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
            {
                CProjectObject obj = CProjectObject.CreateProjectObjectWithName(type.Name);
                FillProperties(type, obj);
                FillFields(type, obj);
                FillMethods(type, obj);
                FillMembers(type, obj);
                FillEvents(type, obj);
                _projectObjects.Add(obj);
            }
        }

        private static void FillEvents(Type type, CProjectObject obj)
        {
            foreach (EventInfo eventInfo in type.GetEvents())
            {
                obj.Events.Add(eventInfo.Name);
            }
        }

        private static void FillMembers(Type type, CProjectObject obj)
        {
            foreach (MemberInfo memberInfo in type.GetMembers())
            {
                obj.Members.Add(memberInfo.Name);
            }
        }

        private static void FillMethods(Type type, CProjectObject obj)
        {
            foreach (MethodInfo methodInfo in type.GetMethods())
            {
                obj.Methods.Add(methodInfo.Name);
            }
        }

        private static void FillFields(Type type, CProjectObject obj)
        {

            foreach (FieldInfo fieldInfo in type.GetFields())
            {
                obj.Fields.Add(fieldInfo.Name);
            }
        }

        private static void FillProperties(Type type, CProjectObject obj)
        {
            foreach (PropertyInfo propertyInfo in type.GetProperties())
            {
                obj.Properties.Add(propertyInfo.Name);
            }
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();
            foreach (CProjectObject obj in _projectObjects) 
            {
                builder.AppendLine(obj.ToString());
            }
            return builder.ToString();
        }

    }

CProjectObject.cs

  class CProjectObject
    {

        private String _name;

        public String Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private List<String> _methods;

        public List<String> Methods
        {
            get { return _methods; }
            set { _methods = value; }
        }

        private List<String> _properties;

        public List<String> Properties
        {
            get { return _properties; }
            set { _properties = value; }
        }

        private List<String> _fields;

        public List<String> Fields
        {
            get { return _fields; }
            set { _fields = value; }
        }

        private List<String> _members;

        public List<String> Members
        {
            get { return _members; }
            set { _members = value; }
        }

        private List<String> _events;

        public List<String> Events
        {
            get { return _events; }
            set { _events = value; }
        }

        public CProjectObject()
        {
            _name = "Anonym";
            _fields = new List<string>();
            _properties = new List<string>();
            _methods = new List<string>();
            _members = new List<string>();
            _events = new List<string>();
        }

        private CProjectObject(String name) : this()
        {
            _name = name;
        }

        public static CProjectObject CreateProjectObjectWithName(String name) 
        {
            return new CProjectObject(name);
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("ObjectName:" + _name);
            builder.AppendLine("-Contains the following fields");
            foreach (String str in Fields) 
            {
                builder.AppendLine("\tField:" + str);
            }
            builder.AppendLine("-Contains the following properties");
            foreach (String str in Properties)
            {
                builder.AppendLine("\tProperty:" + str);
            }
            builder.AppendLine("-Contains the following methods");
            foreach (String str in Methods)
            {
                builder.AppendLine("\tMethod:" + str);
            }
            builder.AppendLine("-Contains the following members");
            foreach (String str in Members)
            {
                builder.AppendLine("\tMember:" + str);
            }
            builder.AppendLine("-Contains the following events");
            foreach (String str in Events)
            {
                builder.AppendLine("\tEvent:" + str);
            }

            return builder.ToString();
        }
    }

答案 3 :(得分:0)

如果只是要有图形视图,您可以使用visual studio并扩展您的解决方案树。

否则,如果你想这样做,那就是使用Reflection,它不是很难掌握。