The problem is that your T
is an object
, because d
has a compile-time type of object
, but t.GetType()
is Dog
, because the runtime type of t
is Dog
.
So you've got an expression which accepts a Dog
, but you're trying to pass it a variable of type object
-- you're missing the cast which would turn the object
into a Dog
.
In other words, you're writing something roughly equivalent to:
public int GeneratedMethod(object o)
{
Dog d = o; // <-- This line will cause a compile-time error, of course
return d.Id;
}
The question is, what do you want your generated method to look like? Should it look like this?
public int GeneratedMethod(object o)
{
Dog d = (Dog)o;
return d.Id;
}
If so, you'll need to insert the cast from object
to Dog
. The way to do this is using Expression.Convert
:
var type = t.GetType();
var arg = Expression.Parameter(typeof(T), "x");
var expr = Expression.Property(Expression.Convert(arg, type), "Id");
var compiled = Expression.Lambda<Func<T, int>>(expr, arg).Compile();
Here we're declaring a lambda which accepts a T
, but then casts it to Dog
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…