I create a Delegate
from an method I know only takes one parameter and later call it using a DynamicInvoke
, but I was wondering if it was possible to get an Action
to invoke directly.
Here is what I have currently:
private Delegate CreateDelegate(MethodInfo method) {
Type requestType = method.GetParameters()[0].ParameterType,
actionType = typeof(Action<>).MakeGenericType(requestType);
return Delegate.CreateDelegate(actionType, this, method);
}
public void Invoke(string json) {
var requestType = MyDelegate.Method.GetParameters()[0].ParameterType;
var o = Deserialize(json, requestType);
MyDelegate.DynamicInvoke(o);
}
Using an Action, not only would it be faster but it would look much neater. The following code doesn't work but there must be a way to get something similar:
private Action CreateAction(MethodInfo method) {
Type requestType = method.GetParameters()[0].ParameterType,
actionType = typeof(Action<>).MakeGenericType(requestType);
return (Action) Delegate.CreateDelegate(actionType, this, method);
}
public void Invoke(string json) {
Type requestType = MyAction.GetParameters()[0].ParameterType;
var o = Deserialize(json, requestType);
MyAction(o);
}
Aucun commentaire:
Enregistrer un commentaire