Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.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_Generics_Inheritance_Covariance - Fatal编程技术网

.net 将泛型列表作为类型参数传递

.net 将泛型列表作为类型参数传递,.net,vb.net,generics,inheritance,covariance,.net,Vb.net,Generics,Inheritance,Covariance,是否有任何方法可以调用main应用程序中的Me.DoSomething2.CallDoSomething以在下面的代码中工作 Public Interface ICovariance(Of Out T As Grandparent) End Interface 我知道DoSomething2上的类型参数需要一个CustomList2(父级),但我想向它传递一个CustomList2(子级)。我可以让它与使用协方差的接口一起工作。使用接口不允许调用方法,例如CustomList2.MyCust

是否有任何方法可以调用main应用程序中的Me.DoSomething2.CallDoSomething以在下面的代码中工作

Public Interface ICovariance(Of Out T As Grandparent) 
End Interface
我知道DoSomething2上的类型参数需要一个CustomList2(父级),但我想向它传递一个CustomList2(子级)。我可以让它与使用协方差的接口一起工作。使用接口不允许调用方法,例如CustomList2.MyCustomerList2Method


这是一个简化的示例,有人能提供一些见解吗?

尝试将第二个泛型类型参数添加到第二个方法中:

Public Function DoSomething2(Of T1 As {Parent}, T2 As {CustomList2(Of T1), New})() As T2
    Dim instance As New T2

    'This method would work if I could call this method
    instance.MyCustomList2Method()

    Return instance
End Function
当它看起来像这样时,你可以这样称呼它:

Me.DoSomething2(Of Child, CustomList2(Of Child))()

我试着让你的代码更具可读性。如果您不喜欢,请随时恢复我的更改。我已经尝试过这条路线,它可以工作,但是它会使函数调用非常长,编写起来非常繁琐。我希望只需要传入“collection”类型,让集合中的泛型类型是继承父类的任何类型。我猜如果不使用反射之类的东西,这是不可能的。恐怕这里不能进行泛型类型推断。
Public Class Child : Inherits Parent
End Class
Public Class CustomList(Of T As Grandparent) : Implements ICovariance(Of T)
End Class
Public Class CustomList2(Of T As Parent)
    Inherits CustomList(Of T)

    Public Sub MyCustomList2Method()
    End Sub
End Class
Public Class MainApplication
    Public Sub CallDoSomething()
        'This Works
        Me.DoSomething(Of CustomList2(Of Child))()

        'This does not work
         Me.DoSomething2(Of CustomList2(Of Child))()
    End Sub

    ' This method works because of the interface and covariance 
    Public Function DoSomething(Of T As {ICovariance(Of Grandparent), New})() As T
        Dim instance As New T

        'This method can't be called because I'm working with an interface
        instance.MyCustomList2Method()
        Return instance
    End Function

    ' This method does not work. 
    ' I want T to be a CustomList2 that contains Parent 
    ' (or anything that inherits Parent)
    Public Function DoSomething2(Of T As {CustomList2(Of Parent), New})() As T
        Dim instance As New T

        'This method would work if I could call this method
        instance.MyCustomList2Method()

        Return instance
    End Function
End Class
Public Function DoSomething2(Of T1 As {Parent}, T2 As {CustomList2(Of T1), New})() As T2
    Dim instance As New T2

    'This method would work if I could call this method
    instance.MyCustomList2Method()

    Return instance
End Function
Me.DoSomething2(Of Child, CustomList2(Of Child))()