vendredi 22 mars 2019

How to invoke methods from reflection with a deserialized json object?

I have a collection of methods obtained through reflection, I know they return nothing and have a single parameter which implements a Request interface. I want to call these methods upon request but I can't figure out how to cast them into Action<Request> to call them with the deserialized json request object.

I can create the delegate like so, where method is the MethodInfo of one of the request handlers:

var requestType = method.GetParameters()[0].ParameterType;
Delegate.CreateDelegate(typeof(Action<>).MakeGenericType(requestType), this, method);

but how can I then cast it into an Action<Request> or how would I go about invoking it otherwise?

Ideally this is how I would Invoke it:

public void Invoke(Request request) 
    => Method(request);





Aucun commentaire:

Enregistrer un commentaire