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?
You will have to do this with reflection:
Dim type = list.[GetType]()
Dim isList = type.IsGenericType AndAlso
type.GetGenericTypeDefinition() = GetType(List(Of ))
Unfortunately, this is not possible.
You need to call GetType() and check IsGenericType and GetGenericTypeDefinition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With