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)

[505 byte] By [CzarEclarinal] at [2007-12-24]
# 1

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.

WilliamBartholomew at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2
So it's noto really possible to have a generic collection of all generic implementation of a generic class?
CzarEclarinal at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

You would have to have a generic collection of a non-generic I believe:

Dim list As New Collection(Of IList)

WilliamBartholomew at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

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.

AlexMoura at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...