从methodinfo创建委托 [英] Create a delegate from methodinfo
问题描述
我有以下情况:
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)
myDelegate(EventArgs.Empty)
Exit Sub
End If
我在myType中输入Repeat_Tastendruck_Control的类型
我在myInfo中获得所需的MethodInfo
如果我现在创建一个委托调用此方法它指向一个非实例宾语。那么......我的错误在哪里?
任何指导都很有用......;)
附加信息:
OnClick-Method的定义:
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)
MyBase.OnClick(e)
End Sub
代表的定义:
the definition of the delegate :
Private Delegate Sub delegate_OnClick(e As EventArgs)
命令myDelegate(EventArgs) .Empty)在方法中调用权限 - 但不在Repeat_Tastendruck_Control引用的Control中。
我的尝试:
...... ............................
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:
..................................
推荐答案
创建委托时指向实例方法,您必须提供Me
对象,该对象将在调用委托时使用。因为您还没有这样做,并且您为throwOnBindFailure
参数指定了False
,您的委托实例将是设置为没有
。
有两种方法可以解决这个问题:
1。要创建绑定到该类的单个实例的委托,请使用CreateDelegate
重载之一接受firstArgument
参数:
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 specifiedFalse
for thethrowOnBindFailure
parameter, your delegate instance will be set toNothing
.
There are two ways you can solve this:
1. To create a delegate bound to a single instance of the class, use one of theCreateDelegate
overloads which accept afirstArgument
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
myDelegate(EventArgs.Empty)
Delegate.CreateDelegate方法(类型,对象,MethodInfo)(系统) [ ^ ]
2。创建一个可以与多个实例一起重用的委托该类,为您的委托提供一个额外的参数来表示对象实例:
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:委托的第一个参数的类型必须是声明方法的类型或派生类型。如果要传递接口,则 MethodInfo
必须来自接口,而不是类。
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.
这篇关于从methodinfo创建委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!