VB Recursive Lambda Sub无法编译

Kra*_*atz 3 vb.net lambda

我创建了以下不会编译的递归lambda表达式,给出了错误

无法从包含"OpenGlobal"的表达式推断出"OpenGlobal"的类型.

            Dim OpenGlobal = Sub(Catalog As String, Name As String)
                             If _GlobalComponents.Item(Catalog, Name) Is Nothing Then
                                 Dim G As New GlobalComponent
                                 G.Open(Catalog, Name)
                                 _GlobalComponents.Add(G)
                                 For Each gcp As GlobalComponentPart In G.Parts
                                     OpenGlobal(gcp.Catalog, gcp.GlobalComponentName)
                                 Next
                             End If
                         End Sub
Run Code Online (Sandbox Code Playgroud)

我正在尝试做什么?

Joe*_*orn 10

问题是类型推断.它无法确定OpenGlobal变量的类型,因为它取决于它自身.如果您设置了显式类型,则可能没问题:

 Dim OpenGlobal As Action(Of String, String) = '...
Run Code Online (Sandbox Code Playgroud)

这个简单的测试程序按预期工作:

Sub Main()
    Dim OpenGlobal As Action(Of Integer) = Sub(Remaining As Integer)
                                               If Remaining > 0 Then
                                                   Console.WriteLine(Remaining)
                                                   OpenGlobal(Remaining - 1)
                                               End If
                                           End Sub

    OpenGlobal(10)
    Console.WriteLine("Finished")
    Console.ReadKey(True)
End Sub
Run Code Online (Sandbox Code Playgroud)