开发者

Casting Generic Types

开发者 https://www.devze.com 2022-12-27 19:32 出处:网络
Public Function CastToT(Of T)(ByVal GenericType(Of Object) data) As GenericType(Of T)开发者_开发技巧
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 does not work. Is there any way to perform this cast if I know that all objects inside data are in fact of Type T?


This is not possible, except for interfaces.


No, you cannot cast it directly. Presumably your GenericType is some kind of a container. You will just have to create a new instance of GenericType(Of T) and copy the contents into it, casting each object from Object to T. If GenericType was a System.Collections.List(Of T) then LINQ makes this very easy.

0

精彩评论

暂无评论...
验证码 换一张
取 消