【发布时间】:2011-08-02 18:42:05
【问题描述】:
我正在尝试使用一个变量,它只是一个字符串列表的列表。 我已经声明如下:
Dim iRows As New List(Of List(Of String))
然后我尝试将它作为参数传递给另一个方法,我已将方法定义如下:
Public Sub Import(ByVal Rows As IList(Of IList(Of String)))
For Each Row As IList(Of String) In Rows
ImportRow(Row)
Next
End Sub
不幸的是,当我尝试运行该代码时,我收到以下错误,它试图将我的变量传递给我的方法。
System.InvalidCastException 未被用户代码处理
Message="无法将类型为“System.Collections.Generic.List1[System.Collections.Generic.List1[System.String]]”的对象转换为类型“System.Collections.Generic.IList1[System.Collections.Generic.IList1[System.String]]”。 "
当我将方法定义更改为使用类型而不是接口时,它可以工作。
Public Sub Import(ByVal Rows As List(Of List(Of String)))
For Each Row As IList(Of String) In Rows
ImportRow(Row)
Next
End Sub
那么,以这种方式使用泛型与接口是不可能的?只要我不嵌套它们,它就可以正常工作。
【问题讨论】:
标签: vb.net string list generics interface