Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Should a nested class in a generic class be considered generic?

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.

like image 850
CodeMangler Avatar asked Jan 28 '09 08:01

CodeMangler


People also ask

Huruf c melambangkan apa?

Logo C merupakan sebuah lambang yang merujuk pada Copyright, yang berarti hak cipta.

C dalam Latin berapa?

C adalah huruf ketiga dalam alfabet Latin. Dalam bahasa Indonesia, huruf ini disebut ce (dibaca [tʃe]).

Bahasa C digunakan untuk apa?

Meskipun C dibuat untuk memprogram sistem dan jaringan komputer namun bahasa ini juga sering digunakan dalam mengembangkan software aplikasi. C juga banyak dipakai oleh berbagai jenis platform sistem operasi dan arsitektur komputer, bahkan terdapat beberepa compiler yang sangat populer telah tersedia.

Bahasa C dibuat pertama kali oleh siapa dan tahun berapa?

Bahasa pemrograman C ini dikembangkan antara tahun 1969 – 1972 oleh Dennis Ritchie. Yang kemudian dipakai untuk menulis ulang sistem operasi UNIX. Selain untuk mengembangkan UNIX, bahasa C juga dirilis sebagai bahasa pemrograman umum.


1 Answers

In short, yes - a type inherits the type arguments from any types that contain it: this is the key to things like List<T>.Enumerator and many other scenarios, etc - it is critical that they share the T from the outer class (not just any T).

The ECMA ref is §25.1:

Any class nested inside a generic class declaration or a generic struct declaration (§25.2) is itself a generic class declaration, since type parameters for the containing type shall be supplied to create a constructed type.

like image 112
Marc Gravell Avatar answered Sep 30 '22 01:09

Marc Gravell