我有以下内容:
Public Class Object1()
...
Public Shared Narrowing Operator CType(ByVal object As Object1) As Object2
Return New Object2(...)
End Operator
...
End Class
Public Class Object2
...
End Class
现在,如果我创建List(Of Object1)
并尝试在具有以下原型的函数中使用:DoSomething(ByVal objects As List(Of Object2))
我得到Value of type 'System.Collections.Generic.List(Of Object1) cannot be converted to 'System.Collections.Generic.List(Of Object2)'
答案 0 :(得分:1)
即使您知道如何将A
类型转换为B
类型,也不知道如何将类型List(Of A)
转换为List(Of B)
。但您可以使用List.ConvertAll
:
dim list1 = new List(Of Object1)
list1.Add(new Object1())
dim list2 as List(Of Object2) ' = list1 doesn't compile
list2 = list1.ConvertAll(Function(obj1) CType(obj1, object2))
这将使用您的转换运算符。