mercredi 12 avril 2017

CreateDelegate for Action with Type parameters

i'm trying to create an Action with reflection. The action I have is something like this.

Action<T, String> someAction(T a, String b) { ... };

I created a method in my class to create the instance of the GenericMethodInfo too be used then in "CreateDelegate". The method is :

public void testMethod<T>(T a, String b) { ... };

And then i try to create my action with reflection

var genericMethod = this.GetType().GetMethod("testMethod").MakeGenericMethod(new Type[] { Type.GetType(myVariableType) };
var actionInstance = Delegate.CreateDelegate(typeof(Action<,>), this, genericMethod);

The problem is that when the last statement is executed (createDelegate) I receive this exception

An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll

Additional information: Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.

Does anyone know how to manage this situation ? Thanks a lot





Aucun commentaire:

Enregistrer un commentaire