我有一个通用类Command(Of T)
命令(部分)定义为:
Public Class Command(Of T As BaseType)
Inherits Command
Public Property Condition As Func(Of T, Boolean)
End Class
我想创建一个所有命令的列表,然后当我递给一个对象A
,拉出与我的A
具有相同泛型类型的所有命令,其中调用Condition(A)
返回true
I can do
Dim B As List(Of BaseType)
B.Add(New DerivedType)
,
Dim C As New List(Of Command(Of BaseType))
C.Add(New Command(Of DerivedType))
抛出转换错误。
我可以让Command
从一个非泛型对象继承(让我们称之为CommandBase
…)
Dim C As New List(Of CommandBase)
C.Add(New Command(Of DerivedType))
工作,但现在我不能回到特定类型的引用。这会得到正确的命令对象:
Dim CommandsOfTypeA = B.Where(function(x) x.GetType.GetGenericArguments(0).FullName = A.GetType.FullName)
但是我现在不知道怎么做……
Dim MatchingCommands = CommandsOfTypeA.Where(function(x) c.Condition(A))
因为CommandsOfTypeA是List(Of Command)
而不是List(Of Command(Of DerivedType))
我错过了什么?
问题是,虽然DerivedType
的实例是BaseType
的实例,但这并不一定意味着Command(Of DerivedType)
的实例就是Command(Of BaseType)
的实例。默认情况下,泛型类型参数不考虑类型层次结构。
在编程语言理论中,这种特性被称为协方差。
.net 4.0对此提供了一些支持,尽管它只能应用于接口,而不能应用于类。这里有一些关于这个主题的文档。
基本上是这样的:
Interface ICovariant(Of Out R)
Function GetSomething() As R
' The following statement generates a compiler error.
' Sub SetSomething(ByVal sampleArg As R)
End Interface
这只在泛型类型只用作方法输入而不用作输出或ref
或output
参数时起作用(如图所示)