Let's assume we have 2 classes
class A{
public abstract int Prop1 {get; set;}
}
and it's child
class Child : A{
[CustomAttr(someval)]
override public int Prop1 {get; set;}
}
so later in my code I need to get this custom attribute's val.
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's return false
because ((MemberExpression)property.Body).Member
it's for some reason A.Prop1
and not Child.Prop1
it's possible to fix with more advanced construction like this
((MemberExpression)property.Body).Expression.Type.GetMember(((MemberExpression)property.Body).Member
.Name)[0].GetCustomAttributes(typeof(CustomAttr), false).Any();
But it's still not clear for me why it's threat this expression as base class initially if it's clearly say that it's expression from Child
class. Could someone explain logic behind this for me?
Aucun commentaire:
Enregistrer un commentaire