Is there a way to create a nested open generic type?

618 Views Asked by At

In C# it is easy to create an open generic type, typeof(IEnumerable<>). Is there a way to create a type that contains an open generic? The following does not work: typeof(IEnumerable<IFoo<>>).

2

There are 2 best solutions below

4
Jon Hanna On BEST ANSWER

Get the two unbound generic types involved, and then use one as the argument to making a generic type from the other:

typeof(IEnumerable<>).MakeGenericType(typeof(IFoo<>))
0
Gusman On

There's no way. These aren't generic but unbound types, these are special types, they can't be instantiated. An unbound generic type can only be used within a direct typeof-expression.

When you use typeof(IEnumerable<IFoo<>>) you aren't using the unbound type in the typeof expression but in the IEnumerable<T> expression.