这是我的问题。
我有这三个界面:
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)
有人可以帮助我吗?
答案 0 :(得分:1)
您对IMetalUnitInfo
的约束要求将T
设为IDaughterPlateOfMetalUnitInfo(Of IOperationInfoBase)
,但您要提供IDaughterPlateOfMetalUnitInfo(Of IOperationInfo)
。
通用协方差旨在解决此问题。您需要对IDaughterPlateOfMetalUnitInfo
进行更改。
Interface IDaughterPlateOfMetalUnitInfo(Of Out T As IOperationInfoBase)
然后,DaughterPlateOfMetalUnitInfo
可以满足约束条件,因为IOperationInfo
比IOperationInfoBase
派生得多。