namespace GenericsTest
{
public class AGenericClass<T>
{
public class NestedNonGenericClass
{
}
}
}
In the example above, should NestedNonGenericClass
be considered a generic class?
The reflection API says it's a generic class, and even hands me the template parameters of the containing class as the template parameters of the nested class.
Type nestedClass = typeof(AGenericClass<int>.NestedNonGenericClass);
Console.Out.WriteLine("IsGeneric: {0}\tHasGenericArguments: {1}",
nestedClass.IsGenericType, nestedClass.GetGenericArguments().Length > 0);
This prints out:
IsGeneric: True HasGenericArguments: True
I don't completely agree with this behaviour. Even if the compiler generates a generic type for NestedNonGenericClass
, I'd like to know if it's a generic because it was declared so, or because it's container is generic.
So, my question is:
Firstly, do you think it's okay to consider a nested class generic because it's container was generic? Why / Why not?
Secondly, do you happen to know some other API that can help me identify only the classes that were declared generic?
P.S: I could not find anything related to this in the ECMA specs for generics (or probably I just glossed over it).
--EDIT--
To add a little more context, I'm working on a sort of Code Generator. And I'm using the reflection API to determine whether a type is generic.
I ran into an issue with Dictionary<TKey, TValue>.KeyCollection
.
For KeyCollection
, the reflection API says that it's generic and hands me over TKey
and TValue
which were declared in the container. So, the generator ends up generating Dictionary<TKey, TValue>.KeyCollection<Tkey, TValue>
The only way I could solve this was by matching up the nested class' template parameters against the container's and eliminating all those that match. But I was wondering if there's a better approach.