Try the following:
class Foo<T> { } class Bar<T> { } Type unboundBar = typeof(Bar<>); Type unboundFoo = typeof(Foo<>); Type boundFoo = unboundFoo.MakeGenericType(new[] { unboundBar }); Console.WriteLine(boundFoo.Name); Conosle.WriteLine(boundFoo.GetGenericArguments().First().Name);
Please note that you cannot write
Type boundFoo = typeof(Foo<Bar<>>)
as the specification explicitly states:
An unrelated generic type can only be used in a typeof expression (ยง7.6.11).
( Bar<> is not used here as a parameter for typeof-expression; rather, it is a general type parameter for typeof-expression parameter.)
However, it is completely legal in the CLR, as shown above, using reflection.
But what are you trying to do? You cannot have instances of unrelated types, so I don't understand.
jason
source share