从类中提取属性、字段和方法名的列表
本文关键字:方法 列表 字段 提取 属性 | 更新日期: 2023-09-27 18:07:54
假设Project
是包含我的c#解决方案的所有类和相关文件的目录。
现在我想获得这些类中使用的所有属性、字段和方法名称的列表。我该怎么做呢?我的第一个猜测是使用正则表达式,但后来我认为它可能非常容易出错。然后我看到了这个,但我不知道它是否适合我的情况,我不知道如何使用它。
那么是否有一个程序可以做到这一点?
如果你还没有解决所有的文件,创建一个解决方案,并使用反射遍历类型和成员。
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
}
}
反射:
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。反射
您可以像其他人所说的那样迭代ExecutingAssembly。如果只需要名称,就使用这个调用
static void Main()
{
CProjectReflector reflector = new CProjectReflector();
Console.WriteLine(reflector.ToString());
Console.ReadLine();
}
你必须创建两个类。在CProjectReflector类中,你必须将它添加到文件的顶部
using System.Reflection;
如果你想了解更多关于什么是反射,你可以访问MSDN
有了反思,你可以做得更多,它真的值得一读。总之,它是关于这个
反射提供描述程序集的对象(类型为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();
}
}
如果只是想要一个图形化的视图,你可以使用visual studio来展开你的解决方案树。
否则如果你想用编程的方式来做,那就使用反射,掌握它并不难。