I have a parent / child class hierarchy, where the parent abstractly declares a row property, and the Child class implements it:
abstract class Parent { public abstract string Value { get; } } class Child : Parent { public override string Value { get { return null; } } }
When I use an expression that explicitly (or implicitly) uses the Child class, I expect the MemberInfo DeclaringType expression to be "Child", but instead it is Parent:
Child child = new Child(); Expression<Func<string>> expression = (() => child.Value); MemberInfo memberInfo = expression.GetMemberInfo(); Assert.AreEqual(typeof(Child), memberInfo.DeclaringType); // FAILS!
The statement fails because DeclaringType is the parent.
Is there something I can do by declaring my expression or consuming it to show the actual use of the Child type?
NOTE: GetMemberInfo () is above as an extension method (I even forgot that we wrote it!):
public static class TypeExtensions { /// <summary> /// Gets the member info represented by an expression. /// </summary> /// <param name="expression">The member expression.</param> /// <returns>The member info represeted by the expression.</returns> public static MemberInfo GetMemberInfo(this Expression expression) { var lambda = (LambdaExpression)expression; MemberExpression memberExpression; if (lambda.Body is UnaryExpression) { var unaryExpression = (UnaryExpression)lambda.Body; memberExpression = (MemberExpression)unaryExpression.Operand; } else memberExpression = (MemberExpression)lambda.Body; return memberExpression.Member; } }
Trinition
source share