Generic parameter: How to check type

If I have the following function:

Private Function AddObject(Of T As MyBaseClass)(pos As PointF, sz As SizeF) As T
End Function

How can I check the type of T inside the function? For example I have 3 classes that inherit from MyBaseClass namely Shape, Label and Rect. I tried the following two versions:

If T Is Rect Then
If TypeOf T Is Rect Then

but both produce errors. What is the correct syntax here?

Jon Skeet
people
quotationmark

If you only need an exact match you can use:

GetType(T) Is GetType(Rect)

If you want to match with inheritance, you can use:

GetType(Rect).IsAssignableFrom(GetType(T))

Although to be honest, if you have to handle each subclass separately, it doesn't feel like an ideal kind of generic method. Can you not add an abstract method in the base class and implement it in each subclass instead?

people

See more on this question at Stackoverflow