我是 System.Linq.Expresions 的新手,我试图找出这段代码有什么问题:
var mc = new MyClass();
ParameterExpression value = Expression.Parameter(typeof(object), "value");
ParameterExpression xParam = Expression.Parameter(typeof(MyClass), "x");
Expression<Func<MyClass, object>> e = x => x.Val;
BlockExpression block = Expression.Block(new[] { xParam, value },
Expression.Assign(e.Body, value));
Expression.Lambda<Action<MyClass, object>>(block, xParam, value).Compile()(mc, 5); //I'm getting exception here when Compile()
...
class MyClass
{
public object Val
{
get;
set;
}
public object OtherVal
{
get;
set;
}
}
我只想构建类似 mc.Val = 5 的东西,假设 MyClass 和 object 参数是 lambda 的参数(我不想使用闭包)