I have an implementation building a delegate handler collection.
public class DelegateHandler
{
internal delegate object delegateMethod(object args);
public IntPtr PublishAsyncMethod(MethodInfo method, MethodInfo callback)
{
RuntimeMethodHandle rt;
try
{
rt = method.MethodHandle;
delegateMethod dMethod = (delegateMethod)Delegate.CreateDelegate
(typeof(delegateMethod), method.ReflectedType, method, true);
AsyncCallback callBack = (AsyncCallback)Delegate.CreateDelegate
(typeof(AsyncCallback), method.ReflectedType, callback, true);
handlers[rt.Value] = new DelegateStruct(dMethod, callBack);
return rt.Value;
}
catch (System.ArgumentException ArgEx)
{
Console.WriteLine("*****: " + ArgEx.Source);
Console.WriteLine("*****: " + ArgEx.InnerException);
Console.WriteLine("*****: " + ArgEx.Message);
}
return new IntPtr(-1);
}
}
I publish using the following:
ptr = DelegateHandler.Io.PublishAsyncMethod(
this.GetType().GetMethod("InitializeComponents"),
this.GetType().GetMethod("Components_Initialized"));
And the method I’m creating a delegate from:
public void InitializeComponents(object args)
{
// do stuff;
}
And the callback method:
public void Components_Initialized(IAsyncResult iaRes)
{
// do stuff;
}
Now, I’ve also already looked at this to get an idea of what I might be doing wrong. The CreateDelegate(…) is causing me to receive:
*****: mscorlib
*****:
*****: Error binding to target method.
What is wrong? The methods reside in a different, non-static public class. Any help would be greatly appreciated.
NOTE: These methods will have parameters and return values. As I understand Action, and Action<T>, this would not be an option.
There are 2 problems.
First, you are passing incorrect arguments to
CreateDelegate. Since you are binding to instance methods, you need to pass the instance to which the delegates will be bound, but you are passingmethod.ReflectedTypeinstead of a reference to an object of the class that declaresInitializeComponentsandComponents_Initialized.Second, the signature of
InitializeComponentsdoes not match the declaration of delegatedMethod. The delegate has anobjectreturn type yetInitializeComponentsreturnsvoid.The following should work: