带有Type参数的CreateDelegate for Action

时间:2017-04-12 08:37:06

标签: c# reflection

我正在尝试使用反射创建一个Action。我的行动是这样的。

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

我在我的类中创建了一个方法来创建GenericMethodInfo的实例,然后在“CreateDelegate”中使用它。方法是:

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

然后我尝试使用反射创建我的动作

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

问题是当执行最后一个语句(createDelegate)时,我收到此异常

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.

有谁知道如何管理这种情况? 非常感谢

0 个答案:

没有答案