I'm trying to use the ExpressionVisitor
to get an overriden member of an expression but it's giving me the base one. What am I missing here?
The following example reproduces this behaviour:
Simple base and derived types.
class Base
{
public virtual string Property { get; set; }
}
class Derived : Base
{
public override string Property { get; set; }
}
I use this expression visitor:
internal class DemoVisitor : ExpressionVisitor
{
private MemberInfo _member;
public static MemberInfo GetMemberInfo(LambdaExpression expression)
{
var visitor = new DemoVisitor();
visitor.Visit(expression);
return visitor._member;
}
protected override Expression VisitMember(MemberExpression node)
{
// invalid member here
//node.Member.DeclaringType.Name.Dump();
_member = _member ?? node.Member;
return base.VisitMember(node);
}
}
Calling it like that
void Main()
{
var derived = new Derived();
var expression = (Expression<Func<string>>)(() => derived.Property);
DemoVisitor.GetMemberInfo(expression).DeclaringType.Name.Dump();
}
gives me Base
instead of Derived
. What do I have to do to get to the overriden member?
I need it because I'm reading its attributes later and it's currently giving me the attributes of the property on the base class instead of the derived one.
Aucun commentaire:
Enregistrer un commentaire