Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 是否可以按名称实例化和调用委托?_.net_Vb.net_Delegates - Fatal编程技术网

.net 是否可以按名称实例化和调用委托?

.net 是否可以按名称实例化和调用委托?,.net,vb.net,delegates,.net,Vb.net,Delegates,我试图弄清楚是否可以通过名称而不是显式地实例化和调用委托。我认为下面的代码解释得相当好……我想接受一个函数名,然后在此基础上实例化委托。在这个示例中,我使用了一个select case,但我想消除它,只使用methodName参数本身 尊敬的……请不要急于告诉我这是疯狂的,我应该采取一些完全不同的方法来解决这一问题。:) 谢谢基思,这正是我想要的。 (不过,BFree的方法在大多数情况下也会起作用) 以下是VB中的工作代码: Public Delegate Sub xxxDelegate()

我试图弄清楚是否可以通过名称而不是显式地实例化和调用委托。我认为下面的代码解释得相当好……我想接受一个函数名,然后在此基础上实例化委托。在这个示例中,我使用了一个select case,但我想消除它,只使用methodName参数本身

尊敬的……请不要急于告诉我这是疯狂的,我应该采取一些完全不同的方法来解决这一问题。:)

谢谢基思,这正是我想要的。 (不过,BFree的方法在大多数情况下也会起作用)

以下是VB中的工作代码:

Public Delegate Sub xxxDelegate()

Sub xxxAnImplementation()

End Sub

Sub zzzDoIt(ByVal xxxImplementerName As String)
    Dim theDelegate As xxxDelegate = CType(System.Delegate.CreateDelegate(GetType(xxxDelegate), Me, xxxImplementerName), xxxDelegate)
    theDelegate.Invoke()
End Sub

Private Sub LoadFunctions()
    Dim thisClass As Type = Type.GetType(Me.GetType.BaseType.FullName.ToString)
    For Each method As MethodInfo In thisClass.GetMethods(System.Reflection.BindingFlags.DeclaredOnly)
        If 1 = 1 OrElse method.Name.Substring(0, 3) = "Get" Then
            Me.ddlCodeSamples.Items.Add(method.Name)
        End If
    Next
End Sub

为什么不使用哈希表而不是Select语句呢?

我不会完全回答这个问题,因为我不确定您所问的是否可行。但是,通过反射,可以调用给定方法名称的方法。即:

    string methodName = "MyMethod";
    MethodInfo method = this.GetType().GetMethod(methodName);
    method.Invoke(this, null);
作品

我的回答只是一个建议

为什么不直接将方法的地址传递给InvokeMethod,而不是以字符串形式传递方法名呢

Module Module1
Sub Main()
    InvokeMethod(AddressOf myDelegate_Implementation1)
    InvokeMethod(AddressOf myDelegate_Implementation2)
End Sub

Public Delegate Sub myDelegate()

Private Sub myDelegate_Implementation1()
    Console.WriteLine("myDelegate_Implementation1")
End Sub
Private Sub myDelegate_Implementation2()
    Console.WriteLine("myDelegate_Implementation2")
End Sub

Public Sub InvokeMethod(ByVal func As myDelegate)
    func()
End Sub
End Module

如果该方法有委托,也可以执行以下操作:

public delegate void MethodDelegate( ... ) ...

//create the delegate we expect
(MethodDelegate) Delegate.CreateDelegate(
    typeof( MethodDelegate ), this, "methodName", true );
C#,我知道,但是在VB中类似的功能也可以使用

通常我会使用@BFree的答案,但在使用事件驱动反射时,这种方法效果很好——我认为如果多次调用结果委托,这种方法会稍微快一点

public delegate void MethodDelegate( ... ) ...

//create the delegate we expect
(MethodDelegate) Delegate.CreateDelegate(
    typeof( MethodDelegate ), this, "methodName", true );