Click here to Skip to main content
15,667,436 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following Situation :

Dim myType As Type = Repeat_Tastendruck_Control.GetType
 Dim myInfo As System.Reflection.MethodInfo = myType.GetMethod("OnClick", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)

 If myInfo IsNot Nothing Then
      Dim myDelegate As delegate_OnClick = [Delegate].CreateDelegate(GetType(delegate_OnClick), myInfo, False)
     'Repeat_Tastendruck_Control.Invoke(myDelegate, Nothing)
     Exit Sub
 End If

I get in myType the Type of Repeat_Tastendruck_Control
I get in myInfo the the required MethodInfo
If I now create a Delegate to call this Method it points to a not instanced object. So ... where is my mistake ?
Any guidance would be usefull ... ;)

Additional Information :

the definition of the OnClick-Method :
Protected Overrides Sub OnClick(e As System.EventArgs)
End Sub

the definition of the delegate :
Private Delegate Sub delegate_OnClick(e As EventArgs)

the command "myDelegate(EventArgs.Empty)" calls the right in method - but not in the Control which is referenced by "Repeat_Tastendruck_Control".

What I have tried:

Updated 5-Sep-16 9:33am
Richard Deeming 5-Sep-16 9:58am    
What's the definition of the OnClick method, and the delegate_OnClick delegate?
Ralf Meier 5-Sep-16 14:32pm    
Thanks for your reply, Richard. I have updated/improved my question.
Ralf Meier 23-Mar-17 7:38am    
Hi Richard,
I'm sorry but I must ask you once more to this question.
For your Information : the code you gave me works well until now - with one exeption (and this is my additional question to it) :

When I work with a Control where I have overrided the Method "OnClick" it works.
But when I work with a Control which give this method (I think it comes from the Protected-attribute) I can't get a Delegate - but the MethodInfo is correct and the same like the MethodInfo from my derived Control.

Could you please point me again ...?
Richard Deeming 23-Mar-17 7:50am    
Probably best to post this as a new question, with an example of the code you're using.
Ralf Meier 23-Mar-17 8:01am    
OK ... I will do my very best ... ;-)

1 solution

When you create a delegate pointing to an instance method, you have to provide the "Me" object which will be used when the delegate is invoked. Because you haven't done that, and you've specified False for the throwOnBindFailure parameter, your delegate instance will be set to Nothing.

There are two ways you can solve this:

1. To create a delegate bound to a single instance of the class, use one of the CreateDelegate overloads which accept a firstArgument parameter:
Dim myDelegate As delegate_OnClick = [Delegate].CreateDelegate(
    GetType(delegate_OnClick),  ' The delegate type
    Repeat_Tastendruck_Control, ' The object instance - "Me"
    myInfo)                     ' The method info


Delegate.CreateDelegate Method (Type, Object, MethodInfo) (System)[^]

2. To create a delegate which can be reused with multiple instances of the class, give your delegate an extra parameter to represent the object instance:
Private Delegate Sub delegate_OnClick(ByVal instance As YourControlType, ByVal e As EventArgs)
Dim myDelegate As delegate_OnClick = [Delegate].CreateDelegate(GetType(delegate_OnClick), myInfo, True)

myDelegate(Repeat_Tastendruck_Control, EventArgs.Empty)

NB: The type of the first parameter for the delegate must be the type where the method is declared, or a derived type. If you want to pass an interface, then the MethodInfo must come from the interface, not the class.
Share this answer
Ralf Meier 6-Sep-16 6:30am    
Hi Richard,
Thanks a lot for your solution and of course also the explanation.
Your suggestion .2 matched to my requirement and I realized it like you explained it.
I was very sure that there was only missing a little bit but I couldn't find it by myself.
(all the points I could give to you)

Again Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900