Why MemberExpression for child overridden property shows parent property

Suppose we have 2 classes

class A{ public abstract int Prop1 {get; set;} } 

and this is a subsidiary

 class Child : A{ [CustomAttr(someval)] override public int Prop1 {get; set;} } 

so in my code i need to get this custom val attribute.

 bool haveCustomAttr(IExpression<Func<Child, int>> property){ return ((MemberExpression)property.Body).Member.GetCustomAttributes(typeof(CustomAttr)).Any(); } 

so when i call this code

 haveCustomAttr(c => c.Prop1) 

it returns false because ((MemberExpression)property.Body).Member for some reason it is A.Prop1 , not Child.Prop1

a more advanced design such as

 ((MemberExpression)property.Body).Expression.Type.GetMember(((MemberExpression)property.Body).Member .Name)[0].GetCustomAttributes(typeof(CustomAttr), false).Any(); 

But it’s still not clear to me why this threatens this expression as a base class if it explicitly says that it is an expression from the Child class. Can someone explain the logic of this for me?

+5
source share

Source: https://habr.com/ru/post/1275402/


All Articles