我有一个类酒吧这样的:反映属性以获取属性。如何在别处定义它们时执行操作?
class Foo : IFoo {
[Range(0,255)]
public int? FooProp {get; set}
}
class Bar : IFoo
{
private Foo foo = new Foo();
public int? FooProp { get { return foo.FooProp; }
set { foo.FooProp= value; } }
}
我需要找到属性[范围(0,255)仅反映在财产Bar.FooProp。我的意思是,当我正在解析时,prop是在类实例(.. new Foo())中进行装饰的,而不是在类中。逸岸Bar.FooProp没有属性
编辑
我感动的接口的定义的属性,所以我要做的就是解析继承的接口来找到它们。我能做到这一点,因为酒吧类必须实现IFoo.In这种特殊情况下,我很幸运,但问题仍然存在,当我有没有接口...我会留意下一次
foreach(PropertyInfo property in properties)
{
IList<Type> interfaces = property.ReflectedType.GetInterfaces();
IList<CustomAttributeData> attrList;
foreach(Type anInterface in interfaces)
{
IList<PropertyInfo> props = anInterface.GetProperties();
foreach(PropertyInfo prop in props)
{
if(prop.Name.Equals(property.Name))
{
attrList = CustomAttributeData.GetCustomAttributes(prop);
attributes = new StringBuilder();
foreach(CustomAttributeData attrData in attrList)
{
attributes.AppendFormat(ATTR_FORMAT,
GetCustomAttributeFromType(prop));
}
}
}
}
事实上,关键在于反映“内部”get/set方法。我不知道是否有一种方法可以理解返回参数是对实例方法的调用。 – 2009-06-20 11:46:35
如果您*内* get/set,那么您已经知道类型...只使用已知类型... ? – 2009-06-20 11:57:17