m-y m-y - 1 month ago 5
C# Question

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

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<>>)
.

Answer

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

typeof(IEnumerable<>).MakeGenericType(typeof(IFoo<>))
Comments