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