Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic TypeOf operator?

Dim x = GetType(List(Of )) 'valid statement
Dim list As New List(Of String)

Now I want to see if list is a List(Of T) variable:

Dim isList = TypeOf list Is List(Of )

On the last line I get a compile error: "Type Expected".

Is there any cheap-performance TypeOf operator alternative for generics?

like image 369
Shimmy Weitzhandler Avatar asked Jul 13 '26 05:07

Shimmy Weitzhandler


2 Answers

You will have to do this with reflection:

Dim type = list.[GetType]()
Dim isList = type.IsGenericType AndAlso
    type.GetGenericTypeDefinition() = GetType(List(Of ))
like image 197
cdhowie Avatar answered Jul 14 '26 19:07

cdhowie


Unfortunately, this is not possible.

You need to call GetType() and check IsGenericType and GetGenericTypeDefinition.

like image 34
SLaks Avatar answered Jul 14 '26 18:07

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!