mercredi 10 mars 2021

C# MethodInfo to Callable Func - Expression Tree with generic return type

I am trying to take a MethodInfo and convert it into a callable Func. I have been successfully able to do this with a synchronous function of with method signature:

public AnyObject MyFuncName(AnotherObject message, IMessage messageMetadata) 

Where AnyObject and AnotherObject is just any class. I have done so with the below code, after extracting the MethodInfo with reflection:

public static Func<Handler, object, IMessage, object> CompileHandler(Type handler, MethodInfo method, Type argumentType)
    {
        var instance = Expression.Parameter(typeof(Handler), "instance");
        var messageParam = Expression.Parameter(typeof(object), "message");
        var messageMetaParam = Expression.Parameter(typeof(IMessage), "messageMetadata");
        var methodCall = Expression.Call(
            Expression.Convert(instance, handler),
            method,
            Expression.Convert(messageParam, argumentType),
            Expression.Convert(messageMetaParam, typeof(IMessage)));

        return Expression.Lambda<Func<Handler, object, IMessage, object>>(
            methodCall,
            instance,
            messageParam,
            messageMetaParam).Compile();
    }

The above works. It will create the Func and I am able to call the function. I am now trying to make the function I am reflecting on asynchronous and will have the exact signature except it will be of return type Task<AnyObject> that gets converted to a Func<Handler, object, IMessage, Task<object>>

However, when I change all references above of Func<Handler, object, IMessage, object> to Func<Handler, object, IMessage, Task<object>>

When I do this, Expression.Lambda throws this exception:

Initialization method HandlerUnitTests.TestInitialize 
threw exception. System.ArgumentException: System.ArgumentException: Expression of type 
'System.Threading.Tasks.Task`1[Handler.AnyObject]' cannot be used for return type 
'System.Threading.Tasks.Task`1[System.Object]'

However it had no such objections when casting the return type to object when the Func signature was Func<Handler, object, IMessage, object>





Aucun commentaire:

Enregistrer un commentaire