Casting Common Types

Public Function CastToT(Of T)(ByVal GenericType(Of Object) data) As GenericType(Of T)
   Return DirectCast(data, GenericType(Of T))
End Function

      

The above clearly doesn't work. Is there a way to do this selection when I know that all objects within the data are actually of type T?

+2


a source to share


2 answers


This is not possible, except for interfaces.



+2


a source


No, you cannot use it directly. Presumably yours GenericType

is some sort of container. You just need to create a new instance GenericType(Of T)

and copy the content into it, discarding every object from Object

to T

. If you GenericType

had System.Collections.List(Of T)

, then LINQ makes it very easy.



+1


a source







All Articles