我有这些简单的界面:
public interface IQuery<TResult> { }
public interface IQueryHandler<in TQuery, out TResult>
where TQuery : IQuery<TResult> {
TResult Handle(TQuery query);
}
并且有一些实施。我正在尝试创建一个表达式树来在指定的处理程序上调用Handle
方法。我的意思是:
var query = new MyQuery(); // which MyQuery implements IQuery<int>
object handler = _someServiceProvider
.Get<IQueryHandler<MyQuery, int>>();
此外,还有MyQueryHandler
:
public class MyQueryHandler : IQueryHandler<MyQuery, int> {
public int Handle(MyQuery query) { return 20; }
}
现在,我正在尝试创建Func<object, MyQuery, int>
来像这样调用:
var func = GetMethod<MyQuery, int>(handler);
var result = func(handler, query);
这是我的GetMethod
实施:
private Func<object, TQuery, TResult> GetMethod<TQuery, TResult>(object obj)
where TQuery : IQuery<TResult> {
var methodInfo = obj.GetType().GetMethod(nameof(IQueryHandler<TQuery, TResult>.Handle));
var insExp = Expression.Parameter(typeof(object), "ins");
var inputExp = Expression.Parameter(typeof(TQuery), "query");
var instanceExp = Expression.Variable(obj.GetType(), "instance");
var assExp = Expression.Assign(instanceExp, Expression.Convert(insExp, obj.GetType()));
var castExp = Expression.Convert(inputExp, methodInfo.GetParameters()[0].ParameterType);
var callExp = Expression.Call(instanceExp, methodInfo, castExp);
var blockExp = Expression.Block(new Expression[] {
insExp,
inputExp,
instanceExp,
assExp,
castExp,
callExp
});
var func =
Expression.Lambda<Func<object, TQuery, TResult>>(
blockExp,
insExp,
inputExp).Compile();
return func;
}
但是,当我尝试编译Lambda
时,我收到此错误:
发生了'System.InvalidOperationException'类型的异常 System.Core.dll但未在用户代码中处理
附加信息:类型的变量'instance' 'namespacespace.MyQueryHandler'从范围''引用,但事实并非如此 定义
我哪里错了?我错过了什么?你有什么主意吗?提前谢谢。
答案 0 :(得分:1)
你在GetMethod
中对你的表达方式所做的事情并不是很清楚,所以我不会使用它并从头开始写。
如果您想将处理程序和查询都传递给您的方法 - 您不需要将任何实例传递给GetMethod
:
private static Func<object, TQuery, TResult> GetMethod<TQuery, TResult>()
where TQuery : IQuery<TResult> {
// "query" paramter
var query = Expression.Parameter(typeof(TQuery), "query");
// "handler" parameter
var handler = Expression.Parameter(typeof(object), "handler");
// convert your "object" parameter to handler type (not type safe of course)
// ((IQueryHandler<TQuery, TResult>) handler).Handle(query)
var body = Expression.Call(Expression.Convert(handler, typeof(IQueryHandler<TQuery, TResult>)), "Handle", new Type[0], query);
//(handler, query) => ((IQueryHandler<TQuery, TResult>) handler).Handle(query);
return Expression.Lambda<Func<object, TQuery, TResult>>(body, handler, query).Compile();
}
object handler = new MyQueryHandler();
var func = GetMethod<MyQuery, int>();
var result = func(handler, query);
如果您 将handler
实例传递给GetMethod
- 您不需要稍后将该同一实例再次传递给您创建的func
- 你可以像这样重复使用相同的实例(假设当然适合你的场景):
private static Func<TQuery, TResult> GetMethod<TQuery, TResult>(object obj)
where TQuery : IQuery<TResult> {
// parameter
var query = Expression.Parameter(typeof(TQuery), "query");
// note Expression.Constant here - you use the same instance for every call
var body = Expression.Call(Expression.Constant(obj), "Handle", new Type[0], query);
return Expression.Lambda<Func<TQuery, TResult>>(body, query).Compile();
}
并使用它:
var query = new MyQuery(); // which MyQuery implements IQuery<int>
object handler = new MyQueryHandler();
var func = GetMethod<MyQuery, int>(handler);
var result = func(query);
答案 1 :(得分:1)
据我所知,你正试图写这个函数:
TResult f(object ins, TQuery query)
{
var instance = (MyQueryHandler)ins;
return instance.Handle(query);
}
要使用表达式树执行此操作,您必须在Expression.Block
中声明变量,但只能指定上面的两个语句,而不是所有子表达式:
var blockExp = Expression.Block(new[] { instanceExp }, new Expression[] {
assExp,
callExp
});
但更简单的选择是编写以下函数:
TResult f(object ins, TQuery query)
{
return ((MyQueryHandler)ins).Handle(query);
}
看起来像这样:
var callExp = Expression.Call(
Expression.Convert(insExp, obj.GetType()), methodInfo, castExp);
var func =
Expression.Lambda<Func<object, TQuery, TResult>>(
callExp,
insExp,
inputExp).Compile();