Search code examples
c#nested-genericsopen-generics

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


Solution

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