Consider the following code:
using System;
using System.Linq.Expressions;
public class Program
{
public static void Main()
{
var propFromReflection = typeof(Bar).GetProperty("Property");
Expression<Func<Bar, int>> expression = bar => bar.Property;
var memberExpr = expression.Body as MemberExpression;
var propFromExpression = memberExpr.Member;
Console.WriteLine("Reflection says " + propFromReflection.DeclaringType.Name);
Console.WriteLine("Expression says " + propFromExpression.DeclaringType.Name);
}
}
public abstract class Foo
{
public abstract int Property { get; set; }
}
public class Bar : Foo
{
public override int Property { get; set; }
}
The output in console will state that
Reflection says Bar
Expression says Foo
Why are they different? How come the Expression engine won't get the exact MemberInfo
instance used within the lambda expression? Technically it has everything it needs to do so, doesn't it?
Aucun commentaire:
Enregistrer un commentaire