Making a lambda expression to call a method from a generic class

c# c#-4.0 expression-trees func lambda

Question

I have these simple interface:

public interface IQuery<TResult> { }

public interface IQueryHandler<in TQuery, out TResult> 
    where TQuery : IQuery<TResult> {
    TResult Handle(TQuery query);
}

And there are some implementation of them. I'm trying to create an expression-tree to call Handle method on a specified handler. I mean:

var query = new MyQuery(); // which MyQuery implements IQuery<int>
object handler = _someServiceProvider
    .Get<IQueryHandler<MyQuery, int>>();

Also, there is a MyQueryHandler:

public class MyQueryHandler : IQueryHandler<MyQuery, int> {
    public int Handle(MyQuery query) { return 20; }
}

Now, I'm trying to create a Func<object, MyQuery, int> to call like this:

var func = GetMethod<MyQuery, int>(handler);
var result = func(handler, query);

And here is my GetMethod implementation:

    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;
    }

But, when I try to compile Lambda, I get this error:

An exception of type 'System.InvalidOperationException' occurred in System.Core.dll but was not handled in user code

Additional information: variable 'instance' of type 'Namespace.MyQueryHandler' referenced from scope '', but it is not defined

Where am I wrong? What I missed? Do you have any idea? Thanks in advance.

1
2
5/21/2017 3:07:17 PM

Accepted Answer

As far as I can tell, you're trying to write this function:

TResult f(object ins, TQuery query)
{
    var instance = (MyQueryHandler)ins;
    return instance.Handle(query);
}

To do this using expression trees, you have to declare the variable in your Expression.Block, but then only specify the two statements above, not all subexpressions:

var blockExp = Expression.Block(new[] { instanceExp }, new Expression[] {
    assExp,
    callExp
});

But a simpler option would be to write the following function instead:

TResult f(object ins, TQuery query)
{
    return ((MyQueryHandler)ins).Handle(query);
}

That would look like this:

var callExp = Expression.Call(
    Expression.Convert(insExp, obj.GetType()), methodInfo, castExp);

var func =
    Expression.Lambda<Func<object, TQuery, TResult>>(
        callExp,
        insExp,
        inputExp).Compile();
1
5/21/2017 6:09:21 PM

Popular Answer

It's not really clear what you are trying to do with your expression in GetMethod, so I will not use it and write completely from scratch.

If you want to pass both handler and query to your method - you don't need to pass any instances to 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);

If you do pass handler instance to GetMethod - you don't need to later pass that same instance again to your created func - you can reuse the same instance like this (assuming that fits your scenario of course):

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();
}

And use it:

var query = new MyQuery(); // which MyQuery implements IQuery<int>
object handler = new MyQueryHandler();
var func = GetMethod<MyQuery, int>(handler);
var result = func(query);


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow