Cast IEnumerable(Of String) to my sub-classed List(Of String)

52 Views Asked by At

I have this class that inherits from a list:

Public Class TextLines : Inherits List(Of String)


End Class

Now, how I can cast a return value of type IEnumerable(Of String) to my custom Class?

Real example of what I'm trying to do:

Dim lines As TextLines
lines = CType(File.ReadAllLines(Me.filepathB, Me.encodingB).ToList, TextLines)

It throws an invalid cast exception.

1

There are 1 best solutions below

2
On BEST ANSWER

Why don't you just do this?

Dim lines As New TextLines()
lines.AddRange(File.ReadAllLines(Me.filepathB, Me.encodingB));

or expose the constructor that takes the IEnumerable<T>?