这里有一种方式来差不多做到这一点。缺少的是使用反射,BindingFlags.FlattenHierarchy不会返回父类的私有方法。将这些类型标记为受保护或公开将解决此问题。 (你也手动进给基座类阅读私有成员)
如果你想找到的组件声明一个给定类型的属性的所有类型,你可以写这样的方法:
// using System.Reflection
public IEnumerable<Type> GetTypesWithPropertyOfType(Assembly a, Type t)
{
BindingFlags propertyBindingFlags = BindingFlags.Public
| BindingFlags.NonPublic
| BindingFlags.Instance
| BindingFlags.FlattenHierarchy;
// a property is kept if it is assignable from the type
// parameter passed in
MemberFilter mf = (pi, crit)=>
(pi as PropertyInfo)
.PropertyType
.IsAssignableFrom(t);
// a class is kept if it contains at least one property that
// passes the property filter. All public and nonpublic properties of
// the class, and public and protected properties of the base class,
// are considered
Func<Type, bool> ClassFilter =
c=>c.FindMembers(MemberTypes.Property, propertyBindingFlags, mf, null)
.FirstOrDefault() != null;
// return all classes in the assembly that match ClassFilter
return
a.GetTypes()
.Where(c=>c.IsClass)
.Where(ClassFilter);
}
要查找在执行组件定义或继承type1
类型的属性等级,你可以拨打:
var v = GetTypesWithPropertyOfType(
Assembly.GetExecutingAssembly(),
typeof(type1));
foreach (var n in v) Console.WriteLine(n.FullName);
此打印出foo1。如果你的代码定义foo的类被修改为(a)作出foo1.prop1
公共或受保护的,和(b)使从foo1
foo2
继承,那么上面的代码打印:
foo1
foo2
foo3
预期。
来源
2012-01-16 11:10:14
drf
如何修改上面的方法来获取类型为“type1”的属性值。请告诉我。我的意图是获取任何类的实例的值。 – rinks 2012-01-17 05:04:34
嘿我得到了另一个解决方案(非反射方法),因为我认为投入这些努力太多了。但感谢您的帮助。 – rinks 2012-01-18 10:54:03