假设Project
是包含我的C#解决方案的所有类和相关文件的目录。
现在,我想获得这些类中使用的所有属性,字段和方法名称的列表。我怎样才能做到这一点?我的第一个猜测是使用正则表达式,但随后我虽然可能很容易出错。然后我看到了this,但是我不知道它是否适合我的情况,我也不知道如何使用它。
那么有没有执行此操作的程序?
最佳答案
反射:
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
}
}
确保添加“使用System.Reflection”