I have a Bar class as follows:
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; } } }
I need to find the [Range (0.255)] attribute reflecting ONLY on the Bar.FooProp property. I mean, prop is decorated with an instance of the class (.. new Foo ()) not in the class when I am currently parsing. Infact Bar.FooProp has no attributes
EDIT
I moved the attributes to the interface definition, so I need to parse the inherited interfaces to find them. I can do this because the Bar class must implement IFoo. In this particular case, I was lucky, but the problem remains when I do not have interfaces ... I will notice next time
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)); } } } }
source share