【发布时间】:2014-11-11 18:13:15
【问题描述】:
我创建了一个泛型类,约束为IComparable,如下:
Public Class Foo(Of T As IComparable)
Private _f As T
Public Sub New(ByVal f As T)
_f = f
End Sub
Public Function GetBar() As Bar
Return New Bar(Me) 'compiler error: Value of type Foo(of T)
'cannot be converted to Foo(of IComparable)
End Function
End Class
我有另一个类,它以定义的第一个类的对象作为构造函数参数:
Public Class Bar
Private _foo As Foo(Of IComparable)
Public Sub New(ByVal foo As Foo(Of IComparable))
_foo = foo
End Sub
End Class
但是,我在尝试创建 Bar 对象时收到所示错误。
谁能解释我为什么会收到这个错误?我希望编译器知道类型 T 是任何 IComparable 类型...
我发现了一个类似的问题here,但我不知道如何在我的情况下使用它。
【问题讨论】:
标签: vb.net generics type-conversion