我有一個類酒吧這樣的:反映屬性以獲取屬性。如何在別處定義它們時執行操作?
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