If you could be assigning different types of generic to this variable then is there a common interface / base class all types support (or could you refactor your code to include one)?
QUOTE]
Yes.
Suppose you have a drawing application with a lot of boxes on the screen. Lines can be drawn between them, which can be straight or curved. All the curved lines between two boxes need to be stored together, so that they can be drawn without overlapping, and similarly with the straight lines. You don't want to store any of the lines with the boxes, because each line relates to two boxes, so the choice of which to store it with would be arbitrary. I'd rather store them in separate collections. So here's the sort of code I'd like to write:
Code:
Public Class Form1
Dim j As List(Of LineCol(Of BasicLine)) 'This is not the generic object to which my query relates.
Friend Sub HandleNewLine(ByVal X As BasicLine, ByVal F As Box, ByVal T As Box)
For Each i As LineCol(Of BasicLine) In j
If i.FromBox Is F And i.ToBox Is T And i.GetBasicType Is X.GetType Then 'GetBasicType doesn't exist.
i.Add(X)
Exit Sub
End If
Next
'No matching LineCol has been found, so create a new one.
Dim ty As Type = X.GetType
Dim w As New LineCol(Of ty) 'Type ty is not defined.
w.Add(X)
j.Add(w)
End Sub
End Class
Friend Class LineCol(Of T As BasicLine)
Inherits CollectionBase
Friend FromBox As Box
Friend ToBox As Box
Friend Sub Add(ByVal value As T)
MyBase.List.Add(value)
End Sub
End Class
Friend Class BasicLine
End Class
Friend Class StraightLine
Inherits BasicLine
End Class
Friend Class CurvedLine
Inherits BasicLine
End Class
Friend Class Box
End Class
Clearly there are lots of ways you can work around to get this to work, but if it could work, I think this would be the most elegant way to do it. Do you see what I mean?