Generic Collection of Genereric Type?
I was wondering if I can have a generic collection of generic type.
Lets just say I have a Generic Class that implements the following interface:
Public Interface IGeneric(of T)
ReadOnly Property Name As String
ReadOnly Property Value As T
End Interface
How can I have a generic collection that will accept ALL types of the generic class . Something like this:
Dim Generics As IList(of IGeneric)
You can do part of what you've asked, a generic list of a generic type:
Dim list As New Collection(Of Collection(Of String))
But, it will only accept the specific type you've specified.
No, what you're asking isn't possible - you need to think of different generic implementations as different types - as such, a collection that will hold all of them has to take a type that either all those different types implement, or a base type.
So your options are either use a collection that takes object, use an interface type that all objects implement (if generic it has to be with the same instanciation, otherwise, they'd be different interfaces, wouldn't they? :) ), or if you want to access common non generic members, you could move them to a non generic base class, and use that class as the collection's type.