When I have this,
public static object Create()
{
return new object();
}
this works:
var m = typeof(Class).GetMethod("Create");
var e = Expression.Call(m);
Func<object> f = Expression.Lambda<Func<object>>(e).Compile();
But when I have this,
public static object Create(Type t)
{
return new object();
}
this fails:
var m = typeof(Class).GetMethod("Create");
var e = Expression.Call(m, Expression.Parameter(typeof(Type)));
var t = Expression.Parameter(typeof(Foo));
Func<object> f = Expression.Lambda<Func<object>>(e, t).Compile();
I get An unhandled exception of type 'System.ArgumentException' occurred in System.Core.dll. Additional information: Incorrect number of parameters supplied for lambda declaration. The parameter t
is just expression for a dummy type Foo
. I think that's irrelevant. Where have I gone wrong here?