我有一个 Expression<Action<T>>
,其中 Action 是函数调用,但函数结果未被使用。让我们考虑以下代码示例:
using System;
using System.Linq.Expressions;
namespace ConsoleApp
{
class Program
{
public class MyArg
{
public int Data { get; set; }
}
public class MyExecutor
{
public bool Executed { get; set; }
public int MyMethod(int simpleArg, MyArg complexArg)
{
int result = simpleArg + complexArg.Data;
this.Executed = true;
return result;
}
}
static void Main(string[] args)
{
Expression<Action<MyExecutor>> expr = t => t.MyMethod(2, new MyArg { Data = 3 });
var executor = new MyExecutor();
Action<MyExecutor> action = expr.Compile();
action(executor);
Console.WriteLine(executor.Executed); // true
}
}
}
可能有许多不同的操作,带有不同数量的参数。在所有情况下,我只有这种类型的expr
,它总是调用一个函数并且该函数总是返回相同的类型,在上面的示例中为int
。
我需要像这样的内容:
static Expression<Func<MyExecutor, int>> ToExpressionOfFunc(Expression<Action<MyExecutor>> expr)
{
// TODO
throw new NotImplementedException();
}
能够像这样进行调用:
Expression<Func<MyExecutor, int>> funcExpr = ToExpressionOfFunc(expr);
Func<MyExecutor, int> func = funcExpr.Compile();
int result = func(executor);
Console.WriteLine(result); // should print 5
我有一种感觉,认为这应该是可能的,但不知从何处开始。在调试中,我看到一个expr.Body.Method,它具有期望的Int32 ReturnType,但不清楚如何正确提取它到新的Expression<Func>
中。
Expression<Action<MyExecutor>> expr = t => t.MyMethod(2, new MyArg { Data = 3 });
和Expression<Func<MyExecutor,int>> ret_expr = t => t.MyMethod(2, new MyArg { Data = 3 });
步骤2:在调试器中查看两者之间表达式树构造的差异。 - Ben Voigt