I'm trying to use a variable that is simply a list of a list of strings. I've declared it as follows:
Dim iRows As New List(Of List(Of String))
Then I'm trying to pass it as a parameter to another method and I've defined the method as follows:
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
Unfortunately, when I try to run that code I get the following error where it tries to pass my variable to my method.
System.InvalidCastException was unhandled by user code
Message="Unable to cast object of type 'System.Collections.Generic.List1[System.Collections.Generic.List
1[System.String]]' to type 'System.Collections.Generic.IList1[System.Collections.Generic.IList
1[System.St开发者_如何学编程ring]]'."
When I change the method definition to use the types rather than the interfaces as follows, it works.
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
So, is it not possible to use generics with interfaces in this way? It works fine as long as I'm not nesting them.
Yes, it is possible to create an IList(Of IList(Of String))
- but a List(Of (List(Of String))
isn't one. Consider that in the latter case you could call
listOfLists.Add(arrayOfStrings)
as a string array implements IList(Of String)
.
Basically this is exactly the same as considering an IList(Of Fruit)
and a List(Of Banana)
- a bunch of bananas isn't a fruit-bowl, because you can't add an apple to it.
In your case, you'd need to create a List(Of IList(Of String))
instead - or declare an IList(Of List(Of String))
.
Now interestingly, you could use a List(Of List(Of String))
as an IEnumerable(Of IEnumerable(Of String))
in .NET 4 due to generic covariance and contravariance - IEnumerable(Of T)
is covariant in T
. However, IList(Of T)
is invariant.
Generic covariance and contravariance is a tricky topic. Eric Lippert has written a great deal about it - using C# as the language rather than VB, but hopefully you can still understand it.
Start by declaring iRows like this:
Dim iRows As New List(Of IList(Of String))
Then, when you add a new List(Of String)
to iRows
, it will implicitly cast appropriately.
Try the following change:
Public Sub Import(ByVal Rows As List(Of List(Of String)))
For Each Row As List(Of String) In Rows
ImportRow(Row)
Next
End Sub
There doesn't appear to be a need to cast from List
to IList
.
精彩评论