简体   繁体   中英

Get generic delegate return type

I got ac# method which receives a generic Delegate as a parameter and after invocation checks the type of the result:

public async Task<TResult> InvokeAction<TResult>(Delegate action, object[] actionArgs = null)
{
...
    var result = action.DynamicInvoke(actionArgs);
    if (result is Task<TResult> task) return await task;
}

Is there a way I can check in advanced if the return type of the delegate parameter is indeed TResult without the need to invoke it first? This without changing the parameter the Func<TResult>

You can use Delegate.Method 's ReturnType :

  Func<int> action = () => 1;
  Delegate a = action;
  Console.WriteLine(a.Method.ReturnType); // prints System.Int32

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM