I have walkthru much post of stackoverflow and msdn and tried their solution , but I still cannot get it works, so I hope anyone can help me out . Thank you very much
First I havent added
MethodInfo mi = instance.GetType().GetMethod(sMethodName, myBindingFlags);
Delegate del = Delegate.CreateDelegate(typeof(ErrorEventHandler), null, mi);
directly call
ei.AddEventHandler(instance, handler);
then the error is
Object of type 'xxxxErrorEventHandler' cannot be converted to type 'xxxxErrorEventHandler'.
Then I follow some post modify my code as below, then raise the error when CreateDelegate
DLL:
public class ErrorEventArgs : EventArgs
{
public string ErrorMsg;
}
public interface IClassA
{
bool Run();
}
public class ClassA : IClassA
{
public delegate void ErrorEventHandler(object sender, ErrorEventArgs data);
public event ErrorEventHandler OnErrorHandler;
public void OnError(object sender, ErrorEventArgs data)
{
if (OnErrorHandler != null)
{
OnErrorHandler(this, data);
}
}
public bool Run()
{
// do something inside DLL
ErrorEventArgs data = new ErrorEventArgs();
data.ErrorMsg = "Hello World";
OnError(this, data)
}
}
EXE:
public delegate void ErrorEventHandler(object sender, ErrorEventArgs data);
void main()
{
Assembly assembly = Assembly.LoadFile("myDLL.dll");
Type[] types = assembly.GetTypes();
for (int i = 0; i < types.Length; i++)
{
Type type = assembly.GetType(types[i].FullName);
if (type.GetInterface("myDLL.IClassA") != null)
{
object obj = Activator.CreateInstance(type);
if (obj != null)
{
MethodInfo methodInfo = obj.GetType().GetMethod("Run");
ErrorEventHandler handler = foo;
BindingFlags myBindingFlags = BindingFlags.Instance | BindingFlags.Public;
EventInfo ei = instance.GetType().GetEvent("OnErrorHandler", myBindingFlags);
MethodInfo mi = instance.GetType().GetMethod("Run", myBindingFlags);
*// System.ArgumentException raised here
// Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.*
Delegate del = Delegate.CreateDelegate(typeof(ErrorEventHandler), null, mi);
ei.AddEventHandler(instance, del);
bool Result = methodInfo.Invoke(instance, null);
}
}
}
public void foo(object sender, ErrorEventArgs data)
{
Console.WriteLine("In Foo!");
}
}
reply to floor 1
Hi Hans, Thanks a lot for your answer, I following your advise to modify my code. If the Callback function declare like this
private static void Callback(string msg)
and inside ClassA event also declare like this
public delegate void ErrorEventHandler(string msg);
public event ErrorEventHandler OnErrorHandler;
public void OnError1(string msg)
{
if (OnErrorHandler != null)
{
OnErrorHandler(msg);
}
}
that works fine, but if declare it like before
private static void Callback(object sender, ErrorEventArgs data)
it comes up error " Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type." when run
Delegate del = Delegate.CreateDelegate(ei.EventHandlerType, null, mycallback);
Do you know why , by the way really really thanks for your help.
error if the modified code at EXE side like:
.......
BindingFlags myBindingFlags = BindingFlags.Instance | BindingFlags.Public;
EventInfo ei = instance.GetType().GetEvent("OnErrorHandler", myBindingFlags);
MethodInfo mi = instance.GetType().GetMethod(sMethodName, myBindingFlags);
var mycallback = typeof(ModuleManager).GetMethod("Callback", BindingFlags.Static | BindingFlags.NonPublic);
Delegate del = Delegate.CreateDelegate(ei.EventHandlerType, null, mycallback);
ei.AddEventHandler(instance, del);
.......
private static void Callback(object sender, ErrorEventArgs data)
{
Console.WriteLine("In Callback!");
}