0

Using method attributes, I get an IEnumerable<Delegate>. I want to call each one, now I use DynamicInvoke and get the result. However, some of the methods can be async and return a Task of something. I don't really care what that something is, I just want serialize it using Newtonsoft.

How can I handle both synchronous and asynchronous methods?

I was thinking of checking if the return type implements Task or Task<T> but since I don't know T I'm not sure this would be a good way of doing it.

Right now, my code does not support asynchronous methods and looks like this:

protected string Invoke(Delegate method, params object[] args) {
   return JsonConvert.SerializeObject(method.DynamicInvoke(args));
}

protected void onRequest(Request request) {
   // Some logic
   Send(Invoke(request.Method, request.Arguments));
}

The methods look like this:

[RequestHandler("sell")]
private async Task<SellResponse> Sell(SellItemRequest request) {
   // Some asynchonous logic
   var response = new SellResponse {
      sucess = true,
      price = request.Price
   };
   return response;
}

[RequestHandler("buy")]
private BuyResponse Buy(BuyItemRequest request) {
   // Some logic
   var response = new {
      sucess = true,
      price = request.Price
   };
   return response;
}
...
Hugo
  • 349
  • 3
  • 6
  • 23
  • can i see the entire method? :) – jPhizzle Jun 03 '19 at 18:25
  • @jPhizzle This is extremely oversimplifed code, I will add more details to the example though. – Hugo Jun 03 '19 at 18:26
  • also... JsonConvert.SerializeObject() returns a string.. so Task would be the one to use.... – jPhizzle Jun 03 '19 at 18:26
  • @jPhizzle: I think what he meas is that `args` is a delegate that can be either synchronous or asynchronous. – Robert Harvey Jun 03 '19 at 18:30
  • @RobertHarvey `method` could either refer back to `Sell` or `Buy` for example, one of which is async and the other isn't. – Hugo Jun 03 '19 at 18:34
  • 1
    [How can I tell if a C# method is async/await via reflection?](https://stackoverflow.com/questions/20350397/how-can-i-tell-if-a-c-sharp-method-is-async-await-via-reflection) – Robert Harvey Jun 03 '19 at 18:41
  • At some point in this process there's got to be something that causes a call to either the synchronous or async method. Can you make the determination there? It's far easier to decide what to do with the return value of a method when you decide to call it, as opposed to passing around a method that could return one thing or an entirely different thing, and then trying to figure out what the method returns after you call it. For example, instead of passing `IEnumerable`, invoke them and then pass around an `IEnumerable`. – Scott Hannen Jun 03 '19 at 19:24
  • @ScottHannen I can't, as seen in the example they return different types, some don't return anything. I wouldn't be asking this question otherwise. – Hugo Jun 03 '19 at 19:30
  • 1
    well, why not make Buy() an async method as well? and whatever other methods you may call. make them all async, that way you can just `JsonConvert.SerializeObject(await method.DynamicInvoke(args));` – jPhizzle Jun 03 '19 at 19:40
  • 1
    @jPhizzle I'm working with a big legacy project, going through 20 something files and all their methods won't pass. It would also, most likely, be a breaking change. – Hugo Jun 03 '19 at 19:43

0 Answers0