ArgumentException: Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.
Why does the following Delegate.CreateDelegate produce a runtime ArgumentException
?
- Because the signature of
DoMagicDel
does not match the signature of the method described by mi.
Any chance to get this working?
Yes, use
MakeGenericType
to match the signature of mi:#region Assembly B public class MyDataObject { public delegate void DoMagicDel<T1>(T1 arg1, string arg2); private static Delegate delDoMagic; public void DoMagic(string s) { var bo = Activator.CreateInstance("Module1", "Module1.MyBusinessObject").Unwrap(); MethodInfo mi = bo.GetType().GetMethod("DoMagic", BindingFlags.Public | BindingFlags.Instance); var doMagicDelType = typeof(DoMagicDel<>).MakeGenericType(bo.GetType()); if (delDoMagic == null) delDoMagic = Delegate.CreateDelegate(doMagicDelType, null, mi); delDoMagic.DynamicInvoke(bo, s); } } #endregion
I hope I'm not too late ...