有没有办法在表达式树中设置'DeclaringType'?

Sun*_*est 6 c# lambda expression-trees linq-expressions

我正在做一个Func - > Expression - > Func转换.如果我从方法(下面的第一个示例)创建Func <>(),它可以正常工作,但是如果我使用表达式树(第二个示例)创建函数,则在访问func2.Method.DeclaringType.FullName时它会因NullReferenceException而失败.这是因为DeclaringType为null.(NJection使用反射,所以我认为这就是它需要DeclaringType的原因.)

如何为通过编译表达式树创建的Func <>填写DeclaringType类型?(也许它不可能?)DeclaringType在第一个例子中设置.

使用方法中的Func <> ...(效果很好)

// Build a Func<> 
Func<int, int> add = Add;
// Convert it to an Expression using NJection Library
Expression<Func<int, int>> expr = ToExpr<Func<int, int>>(add);
// Convert it back to a Func<>
Func < int, int> func = expr.Compile();
// Run the Func<>
int result = func(100);
Run Code Online (Sandbox Code Playgroud)

使用表达式树(不起作用)......

// Build a Func<> using an Expression Tree
ParameterExpression numParam = Expression.Parameter(typeof(int));
ConstantExpression five = Expression.Constant(5, typeof(int));
BinaryExpression numAddFive = Expression.Add(numParam, five);
Func<int, int> func2 = 
    Expression.Lambda<Func<int, int>>(
        numAddFive, 
        new ParameterExpression[] { numParam }).Compile();
// Convert to an Expression using NJection (EXCEPTION ON NEXT LINE)
// ToExpr is trying to access func2.Method.DeclaringType.FullName(DeclaringType is null)
Expression<Func<int, int>> exp2 = ToExpr<Func<int, int>>(func2);
// Convert it back to a Func<>
Func<int, int> func2b = exp2.Compile();
// Run the Func<>
int result2 = func2b(100);
Run Code Online (Sandbox Code Playgroud)

tor*_*vin 4

我不知道 NJection 库的用途是什么,因为他们的网站已关闭,并且他们的 Codeplex 上没有可用的源代码。

如果您只需要获得Expression可以编译回函数的 an ,您可以自己创建它。例如这样:

static Expression<T> ToExpr<T>(T func)
{
    var type = typeof(T);
    var method = type.GetMethod("Invoke"); // Delegate.Invoke() has parameters types matching delegate parameters types
    var pars = method.GetParameters()
        .Select(pi => Expression.Parameter(pi.ParameterType))
        .ToArray();

    return Expression.Lambda<T>(
        Expression.Call(Expression.Constant(func), method, pars),
        pars
    );
}
Run Code Online (Sandbox Code Playgroud)

有了这个ToExpr,上面的代码就可以毫无问题地编译和运行。