dimanche 24 juin 2018

When to await a dynamically invoked method?

My code does this to invoke a registered method when a network requests comes in. It's part of an RPC system.

Delegate method;
object result = method.DynamicInvoke(args);

When the method returns a value, everything is good. But when it returns something awaitable, I'm not interested in trying to give that awaitable something back to the network caller. They won't make any use of it, and it's probably not transferrable anyway. Instead I need to wait for it and then return the actual result.

I could just check if the method's return type is something like a Task. Or a Task<T>. Is that enough? Can't there be other awaitable things in the universe? How would I test for the type Task? I mean I can't just do something like this:

method.Method.ReturnType is Task

It won't be true. And there's also no:

method.Method.ReturnType.IsAwaitable

I acknowledge that I won't be able to wait for an async void method. But anything else, eventually awaitable, should be awaited.





Aucun commentaire:

Enregistrer un commentaire