.net - 通用接口和遗产
问题描述
这是我的问题。
我有这 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)
有谁能够帮我 ?
解决方案
IMetalUnitInfo
您对requires T
to be的约束IDaughterPlateOfMetalUnitInfo(Of IOperationInfoBase)
,但您正在提供它IDaughterPlateOfMetalUnitInfo(Of IOperationInfo)
。
通用协方差旨在解决这个问题。您需要对IDaughterPlateOfMetalUnitInfo
.
Interface IDaughterPlateOfMetalUnitInfo(Of Out T As IOperationInfoBase)
那么,DaughterPlateOfMetalUnitInfo
可以满足约束,因为IOperationInfo
比 更派生IOperationInfoBase
。
推荐阅读
- rest - “另一个正在进行的操作”的 HTTP 错误代码
- angular - 缓存后如何以角度访问RouteReuseStrategy中的任何组件数据?
- azure - U-SQL 错误:提供的 Script is 135823414 bytes long, 超过了 2097152 bytes 的大小限制
- node.js - axios找不到isAxiosError.js
- python - XRAY_我们能否使用 Xray 映射一个具有多个标签的机器人测试用例(Jira 中的多个测试用例)
- mongodb - 如何将跟踪上下文传送到 MongoDB 观察者?
- javascript - 我正在使用节点并尝试使用套接字但一次又一次地出错
- prometheus - Loki 中的偏移修改器似乎不起作用
- php - 如何在codeigniter中进行分页
- angular - 由于 zone.js 问题,Angular 11 中的搜索过滤器无法正常工作