通用接口和传承

时间:2018-11-28 09:20:25

标签: .net vb.net interface

这是我的问题。

我有这三个界面:

IOperationInfoBase

IDaughterPlateOfMetalUnitInfo(Of T As IOperationInfoBase)

IMetalUnitInfo(Of T As IDaughterPlateOfMetalUnitInfo(Of IOperationInfoBase))

另一个是从IOperationInfoBase继承的:

Public Interface IOperationInfo
  Inherits IOperationInfoBase

然后这两个类:

Public Class DaughterPlateOfMetalUnitInfo
Implements IDaughterPlateOfMetalUnitInfo(Of IOperationInfo)

Public Class MetalUnitInfo
Implements IMetalUnitInfo(Of DaughterPlateOfMetalUnitInfo)

这第一件事是对的:

Public Property PlateName() As String Implements IDaughterPlateOfMetalUnitInfo(Of IOperationInfo).DaughterPlateName

但这不起作用:

Public Property MetalUnitName() As String Implements IMetalUnitInfo(Of DaughterPlateOfMetalUnitInfo).MetalUnitName

我遇到了以下错误:

  

类型参数'DaughterPlateOfMetalUnitInfo'不继承或实现约束类型'IDaughterPlateOfMetalUnitInfo(Of IOperationInfoBase)

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

您对IMetalUnitInfo的约束要求将T设为IDaughterPlateOfMetalUnitInfo(Of IOperationInfoBase),但您要提供IDaughterPlateOfMetalUnitInfo(Of IOperationInfo)

通用协方差旨在解决此问题。您需要对IDaughterPlateOfMetalUnitInfo进行更改。

Interface IDaughterPlateOfMetalUnitInfo(Of Out T As IOperationInfoBase)

然后,DaughterPlateOfMetalUnitInfo可以满足约束条件,因为IOperationInfoIOperationInfoBase派生得多。