<分区>
当我有这个的时候,
public static object Create()
{
return new object();
}
这个有效:
var m = typeof(Class).GetMethod("Create");
var e = Expression.Call(m);
Func<object> f = Expression.Lambda<Func<object>>(e).Compile();
但是当我有这个的时候,
public static object Create(Type t)
{
return new object();
}
这失败了:
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();
我得到 System.Core.dll 中出现类型为“System.ArgumentException”的未处理异常。附加信息:为 lambda 声明提供的参数数量不正确。参数 t
只是一个虚拟类型 Foo
的表达式。我认为这无关紧要。我哪里出错了?