(双)递归通用参数

时间:2014-01-02 09:25:14

标签: .net vb.net generics inheritance constraints

我正在尝试将两个接口链接在一起,如下所示:

Public Interface A(Of Out T As B(Of A(Of T)))
    ...
End Interface
Public Interface B(Of Out T As A(Of B(Of T)))
    ...
End Interface

问题是我收到错误'Type参数A(Of T)没有继承或实现约束类型A(Of B(Of A(Of T)))',但为什么不呢?

T继承自B(Of A(Of T))并且是Out通用类型,对吗?

UPDATE:这种构造的原因是,在A的实现时,我想通过设置类型参数{将此类型链接到另一个类型B。 {1}}接口T,其类型参数为原始类,如下所示:

B

接口每个都有一个功能:

Class AA
    Implements A(Of BB)
    ...
End Class
Class BB
    Implements B(Of AA)
    ...
End Class

函数Public Interface A(Of Out T As B(Of A(Of T))) Function getB() As T End Interface Public Interface B(Of Out T As A(Of B(Of T))) ReadOnly Property myA As T End Interface 返回getB的一个实例,函数B返回myA的链接实例。也许还有另一种设计方法,但我仍然想知道我得到的错误实际意味着什么。希望有人理解为什么我会收到此错误。

2 个答案:

答案 0 :(得分:0)

我认为你对自己这么努力。为什么要将接口A和B转换为通用类型?我想这是因为你希望方法myA和getB的实现具有与返回的具体类实例匹配的返回类型(即AA和BB)。问问自己为什么需要这个;在接口A或B中缺少AA或BB类的公共/朋友方法?如果是这样,那么这可能是代码气味的标志;你可能想要重构。

罗伯特·C·马丁的一些明智的话:“取决于抽象。不要依赖于结核。”

Public Interface A
    Function getB() As B
End Interface

Public Interface B
    ReadOnly Property myA As A
End Interface

Public Class AA
    Implements A

    Public Function getB() As B       ' the return type is still B
        Return New BB()               ' but the return value is an instance of BB
    End Function
End Class

Public Class BB
    Implements B

    Public ReadOnly Property myA As A ' the return type is still A
        Get
            Return New AA()           ' but the return value is an instance of AA
        End Get
    End Property
End Class

至于你的最后一个问题:我理解你的好奇心,但是更多地挖掘错误的确切原因是浪费时间。在我自己的痛苦经历中,当你得到这样的错误时,它只是意味着你走错了路;重新考虑并重构你的代码!

答案 1 :(得分:0)

你可以做我认为你想要的事情:

Public Interface A(Of TA As A(Of TA, TB), Out TB As B(Of TA, TB))
    Function GetB() As TB
End Interface

Public Interface B(Of Out TA As A(Of TA, TB), TB As B(Of TA, TB))
    ReadOnly Property myA() As TA
End Interface

然后像这样定义你的类:

Public Class AA
    Implements A(Of AA, BB)
    Public Function GetB() As BB Implements A(Of AA, BB).GetB
        Return New BB()
    End Function
End Class

Public Class BB
    Implements B(Of AA, BB)
    Public ReadOnly Property myA() As AA Implements B(Of AA, BB).myA
        Get
            Return New AA()
        End Get
    End Property
End Class

这可以编译并适合我。

您甚至可以使用抽象类来实现它,以消除Implements语法,如下所示:

Public MustInherit Class XA(Of TA As A(Of TA, TB), TB As B(Of TA, TB))
    Implements A(Of TA, TB)
    Public MustOverride Function GetB() As TB Implements A(Of TA, TB).GetB
End Class

Public MustInherit Class XB(Of TA As A(Of TA, TB), TB As B(Of TA, TB))
    Implements B(Of TA, TB)
    Public MustOverride ReadOnly Property myA() As TA Implements B(Of TA, TB).myA
End Class

Public Class AA
    Inherits XA(Of AA, BB)
    Public Overrides Function GetB() As BB
        Return New BB()
    End Function
End Class

Public Class BB
    Inherits XB(Of AA, BB)
    Public Overrides ReadOnly Property myA() As AA
        Get
            Return New AA()
        End Get
    End Property
End Class