【发布时间】:2023-01-26 05:12:36
【问题描述】:
我已经尝试了几个小时并进行了大量重新编码,但可以摆脱 CA1067 违规。
使用:
Visual Studio 2022、.Net v6.0.13、VB.NET
对于解决问题的任何帮助以及对我做错事的见解,我将不胜感激。
所以案例如下:
我有一个模板类SimNode(Of P,A),其中P代表父节点的数据类型,A代表节点三个属性的数据类型。
Public Class SimNode(Of P, A)
Implements ISimNode(Of P, A)
Implements IEquatable(Of SimNode(Of P, A))
'[a bunch of properties and methods]
Public Overridable Shadows Function Equals(other As SimNode(Of P, A)) As Boolean Implements IEquatable(Of SimNode(Of P, A)).Equals
If Not Parent.Equals(other.Parent) Then Return False
If Depth <> other.Depth Then Return False
....
Return True
End Function
End Class
然后我需要创建另一个名为 SimNode 的类,它继承自 SimNode(UShort,UShort) 并需要一个 IEquatable(Of SimNode),因为只有唯一的 SimNode 实例将被添加到模板“容器”-> Container(Of T as IEquatable(Of T)) 中。
容器这个词是通用的,它可以是例如列表、字典或哈希集。
这个新类与父类完全相同,但多了一个成员(列表)。
Private Class SimNode
Inherits SimNode(Of UShort, UShort)
Implements IEquatable(Of SimNode)
'[a bunch of properties and methods]
Private Shadows Function Equals(other As SimNode) As Boolean Implements IEquatable(Of SimNode).Equals
Return MyBase.Equals(other)
End Function
End Class
尽管有额外的列表,但我的平等标准仍然与父类中的相同。
这种方法会导致 CA1067 违规,我就是无法纠正。
我将非常感谢任何帮助!
我尝试遵循 Visual Studio 的建议,但都会导致错误。在子类 (SimNode) 中覆盖方法 Equals 的建议将产生明显的错误,因为它不能覆盖基类,因为它们具有不同的签名。
我也解决了这个https://stackoverflow.com/questions/2441346/cascading-iequatableof-t 没有成功。
【问题讨论】:
-
您似乎没有在任何一个类中覆盖
Equals(Object),这就是 CA1067 的内容。 (我还要提到,非泛型类型派生自同名的泛型类型是很奇怪的;撤销相当普遍,例如对于IEnumerable。) -
谢谢你的回答!这可能很愚蠢,但您可能知道得更多。当我确切知道类型时,覆盖 Equals(object) 有什么意义?
-
您可以重载它,例如
Public Overloads Function Equals(other As SimNode(Of P, A)) As Boolean Implements IEquatable(Of SimNode(Of P, A)).Equals ...,但不能隐藏它。如果需要,您可以覆盖Function Equals(obj As Object) As Boolean -
但你可能应该这样做,在某些用例中是必需的
-
我怀疑如果有人将
Equals(Object)重载称为更具体的重载,你实际上不想给出不同的答案......
标签: vb.net generics inheritance iequatable