首页 > 解决方案 > 通用接口和遗产

问题描述

这是我的问题。

我有这 3 个接口:

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)

有谁能够帮我 ?

标签: .netvb.netinterface

解决方案


IMetalUnitInfo您对requires Tto be的约束IDaughterPlateOfMetalUnitInfo(Of IOperationInfoBase),但您正在提供它IDaughterPlateOfMetalUnitInfo(Of IOperationInfo)

通用协方差旨在解决这个问题。您需要对IDaughterPlateOfMetalUnitInfo.

Interface IDaughterPlateOfMetalUnitInfo(Of Out T As IOperationInfoBase)

那么,DaughterPlateOfMetalUnitInfo可以满足约束,因为IOperationInfo比 更派生IOperationInfoBase


推荐阅读