将表达式<Action<T>> 转换为表达式<Func<T>>
Convert Expression<Action<T>> to Expression<Func<T>>
我有 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
,它总是调用一个函数,并且该函数总是 returns 相同的类型,在我上面的例子中它是 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,所需的 ReturnType 为 Int32,但不清楚如何将其正确提取到新的 Expression<Func>
.
很简单,只需使用现有表达式的正文和参数创建一个新的 Expression<Func<MyExecutor, int>>
:
static Expression<Func<MyExecutor, int>> ToExpressionOfFunc(Expression<Action<MyExecutor>> expr)
{
return Expression.Lambda<Func<MyExecutor, int>>(expr.Body, expr.Parameters);
}
请注意,如果 expr
不是 return 类型 int
,则会引发异常。
我有 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
,它总是调用一个函数,并且该函数总是 returns 相同的类型,在我上面的例子中它是 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,所需的 ReturnType 为 Int32,但不清楚如何将其正确提取到新的 Expression<Func>
.
很简单,只需使用现有表达式的正文和参数创建一个新的 Expression<Func<MyExecutor, int>>
:
static Expression<Func<MyExecutor, int>> ToExpressionOfFunc(Expression<Action<MyExecutor>> expr)
{
return Expression.Lambda<Func<MyExecutor, int>>(expr.Body, expr.Parameters);
}
请注意,如果 expr
不是 return 类型 int
,则会引发异常。