扩展通用约束

时间:2013-12-09 16:57:12

标签: .net vb.net generics inheritance

请参阅下面的代码。命名空间表示DLL分段而不是同一DLL中的命名空间

Public Interface ICoreTableCollection
End Interface

Public Interface ICoreTableInstance
End Interface

Namespace Address.Tier1
    Public MustInherit Class AddressCollection(Of T As AddressInstance)
         Implements ICoreTableCollection
    End Class

    Public MustInherit Class AddressInstance
    End Class
End Namespace

Namespace PartnerAddress.Tier1
    Public MustInherit Class PartnerAddressCollection(Of T As PartnerAddressInstance)
         Implements ICoreTableCollection
    End Class

    Public MustInherit Class PartnerAddressInstance
        MustOverride Function Parent(Of T As {ICoreTableCollection, New}, S As {ICoreTableInstance, New})() As S
    End Class
End Namespace

Namespace Address.Tier2
    Public Class AddressCollection(Of T As AddressInstance)
        Inherits Address.Tier1.AddressCollection(Of T)
    End Class

    Public Class AddressInstance
        Inherits Address.Tier1.AddressInstance
    End Class
End Namespace

Namespace PartnerAddress.Tier2
Public Class PartnerAddressCollection(Of T As PartnerAddressInstance)
    Inherits Tier1.PartnerAddressCollection(Of T)
End Class

Public Class PartnerAddressInstance
    Inherits Tier1.PartnerAddressInstance

    Public Overrides Function Parent(Of T As {New, ICoreTableCollection}, S As {New, ICoreTableInstance})() As S

    End Function

    '*****************
    'This is what I want, but it's not allowed
    '*****************
    'Public Overrides Function Parent(Of T As {New, Address.Tier2.AddressCollection(Of S)}, S As {New, Address.Tier2.AddressInstance})() As S

    'End Function
End Class
End Namespace

无论如何都要在Tier2.PartnerAddress中扩展Parent函数约束,使它看起来像这样。

'*****************
'This is what I want, but it's not allowed
'*****************
Public Overrides Function Parent(Of T As {New, Address.Tier2.AddressCollection(Of S)}, S As {New, Address.Tier2.AddressInstance})() As S

End Function

而不是

Public Overrides Function Parent(Of T As {New, ICoreTableCollection}, S As {New, ICoreTableInstance})() As S

End Function

2 个答案:

答案 0 :(得分:0)

您无法修改已覆盖的类中的通用约束。你可以做的是添加泛型类型。作为简化示例,而不是

Public Class BaseClass
    Public Overridable Sub Something(Of T As ICoreTableCollection)()

    End Sub
End Class

Public Class SubClass
    Inherits BaseClass
    ' doesn't compile:
    'Public Overrides Sub Something(Of T As AddressCollection)()

    'End Sub
End Class

使用

Public Class BaseClass(Of T As ICoreTableCollection)
    Public Overridable Sub Something()

    End Sub
End Class

Public Class SubClass(Of T As AddressCollection)
    Inherits BaseClass(Of T)
    Public Overrides Sub Something()

    End Sub
End Class

(这些示例使用以下类/接口)

Public Interface ICoreTableCollection
End Interface
Public MustInherit Class AddressCollection
    Implements ICoreTableCollection
End Class

答案 1 :(得分:0)

您的问题归结为覆盖如何工作。请考虑以下示例:

Public MustInherit Class AAA
  MustOverride Function ConvertToString(obj As MyObject) As String
End Class
Public Class BBB : Inherits AAA
  Public Overrides Function ConvertToString(obj As MyObject) As String 'this line
    Return String.Empty
  End Function
End Class
Public Class MyObject
End Class
Public Class MyInheritedObject : Inherits MyObject
End Class

现在它显示this line,请注意您无法将MyObject更改为MyInheritedObject。您的是泛型/类型,但适用相同的原则。这是每个OOP,因此您的类需要具有与基础相同的方法签名。

但是,您可以隐藏方法并使用您想要的任何签名。