我想在我的泛型类中做一些算术加法,并有一些约束。为简洁起见,假设只有一个名为 的公共成员Value
。
Public MustInherit Class GenericInterval(Of T As {Structure, IComparable})
Public Value As T
'(1) filled in below
End Class
在类之外,我有一个接口和一个实现它的结构,如下所示:
Public Interface IAddable(Of U As {Structure, IComparable})
Function Add(a As U, b As U) As U
End Interface
Public Structure Int64Calculator
Implements IAddable(Of Int64)
Public Function Add(ByVal a As Int64, ByVal b As Int64) As Int64 _
Implements IAddable(Of Int64).Add
Return a + b
End Function
End Structure
这使我能够在我的班级中在上面标记为 (1) 的位置创建一个函数。有一些限制,但据我所知,我认为这部分是正确的。New
onC
是必需的,否则As C = New C()
不可能。
Public MustInherit Class GenericInterval(Of T As {Structure, IComparable})
Public Value As T
'(2) filled in below
Private Function Add(Of U As {Structure, IComparable},
C As {IAddable(Of U), New}) _
(ByVal a As U, ByVal b As U) As U
Dim calc As C = New C()
Return calc.Add(a, b)
End Function
End Class
现在我打算用这个函数在类的重写GetHashCode
函数中进行计算,如下:
Public MustInherit Class GenericInterval(Of T As {Structure, IComparable})
Public Value As T
Public Overrides Function GetHashCode() As Integer
Const HASHPRIME_1 As Int64 = 4294967291 '32 bits.
Dim lCompHash As Int64 'Partial hash for actual component.
lCompHash = Add(Of T, Int64Calculator)(Value, HASHPRIME_1)
End Function
'... as above
End Class
但是,VS 抱怨错误BC32044,波浪线下划线指的是
Add(Of T, Int64Calculator)
陈述
“类型参数‘Int64Calculator’不继承或实现约束类型‘IAddable(Of T As {Structure, IComparable})’”。
只是我认为该结构Int64Calculator
确实通过Implements IAddable(Of Int64)
.
我想念什么?