表达式树中的错误:System.InvalidOperationException:'A' 类型的变量 'message' 从范围 '' 引用,但未定义

Error in expression tree: System.InvalidOperationException: variable 'message' of type 'A' referenced from scope '', but it is not defined

我需要构建 Action 来代表这段代码: (new P()).Handle(argument type of A)

我有一个表达方式:

Expression.Lambda<Action<A>>(Expression.Call(Expression.New(typeof(P)),
typeof(P).GetMethod("Handle", 
  BindingFlags.Instance | BindingFlags.Public), 
  Expression.Parameter(typeof(A), 
  "message")), 
Expression.Parameter(typeof(A), "message"))
.Compile();

但是当我试图编译它时它抛出一个错误。错误是:

System.TypeInitializationException: The type initializer for 'PerformanceBenchma rk.Test' threw an exception. ---> System.InvalidOperationException: variable 'message' of type 'PerformanceBenchmark.A' referenced from scope '', but it is not defined

我的代码如下所示:

public class A
{
}

public interface IInt<T>
{
    void Handle(T item);
}

public class P : IInt<A>
{
    public void Handle(A item)
    {
        return;
    }
}

public class Test
{
    public static readonly Action<A> _expr = Expression.Lambda<Action<A>>(Expression.Call(Expression.New(typeof(P)), typeof(P).GetMethod("Handle", BindingFlags.Instance | BindingFlags.Public), Expression.Parameter(typeof(A), "message")), Expression.Parameter(typeof(A), "message")).Compile(); 
}

我的目标是测量 _expr(new A()) 的调用速度。但是现在表达式编译失败了。

问题是您调用了 Expression.Parameter 两次,因此您有两个不同的参数表达式。不幸的是,它们不按名称绑定。

所以解决办法就是简单地使用多个语句,创建一个ParameterExpression 一次然后使用它两次。这样代码也更容易阅读:

var method = typeof(P).GetMethod("Handle", BindingFlags.Instance | BindingFlags.Public);
var parameter = Expression.Parameter(typeof(A), "message");
var ctorCall = Expression.New(typeof(P));
var methodCall = Expression.Call(ctorCall, method, parameter);
var expressionTree = Expression.Lambda<Action<A>>(methodCall, parameter);
var compiled = expressionTree.Compile();

当然,要使用该代码初始化静态字段,您需要将其放入辅助方法或静态构造函数中。