Using a class nested in a generic class as type parameter in C#

jnovo

Having the following definition:

public class Generic<T>
{
    public class Nested { }
}

And given that ECMA ref §25.1 states:

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.

I understand that Nested requires the type parameter in order to be instantiated.

I can obtain the generic Type with typeof:

var type = typeof(Generic<>.Nested); 
// type.FullName: Namespace.Generic`1+Nested

Is there any way I can use it as a type parameter for a generic method such as the following?

var tmp = Enumerable.Empty<Generic<>.Nested>(); 
// Error: Unexpected use of an unbound generic

As stated before, from the ECMA specification I understand that all the type parameters must be satisfied before instancing but no objects are being created here. Furthermore, the Nested class does not make use of the type parameters in any way: I simply would like to define it nested for code organization purposes.

René Vogt

No, you can't do that. As you said

all the type parameters must be satisfied before instancing

and though no instance of Generic<>.Nested is actually generated

  • the compiler does not know the semantics of Empty (so doesn't know that no instance of Generic<>.Nested is created)
  • and the main problem: you do want to create an instance of IEnumerabe<Generic<>.Nested>, which would be a type with "unsatisfied type parameters", too

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using a class nested in a generic class as type parameter in C#

From Dev

Using generic class as type parameter

From Dev

Using a interface as a type parameter of generic class with the limitation of "class" in C#

From Dev

Using static nested class as generic parameter

From Dev

Using super class as the type parameter in generic subtype

From Dev

Scala: refer to nested type of class, which comes as type parameter to generic

From Dev

Generic class using generic parameter

From Dev

C# Break down type parameter in generic method if its a nested class

From Dev

c# get the name of the generic type parameter in generic class

From Java

Type Parameter Class with generic java

From Dev

Generic class as parameter with inner type

From Dev

Generic class as parameter with inner type

From Dev

Using generic class as a parameter in method

From Dev

Force a generic type parameter to be a class type?

From Dev

Avoid passing generic type parameter of class extending generic class

From Dev

Avoid passing generic type parameter of class extending generic class

From Dev

Using class object as generic type

From Dev

How to return a generic type from a nested class using a method in the main class

From Dev

Does the name in a parameter type determine if the class is generic?

From Dev

Passing parameter to the constructor of a generic Class type

From Dev

Chose generic implementation if the type parameter is struct or class

From Dev

Generic class E with type parameter to be a subclass of E

From Dev

Generic Class:: Type Parameter <T> Property Inheritance

From Dev

Passing a class with type parameter as type parameter for generic method in Java

From Dev

Use derived class type as parameter of generic action of base class

From Dev

Nested type as template parameter of base class

From Dev

Construct class using string name for generic parameter

From Dev

Generic class with method having generic parameter of different type

From Dev

Accessing a class nested in a generic class

Related Related

  1. 1

    Using a class nested in a generic class as type parameter in C#

  2. 2

    Using generic class as type parameter

  3. 3

    Using a interface as a type parameter of generic class with the limitation of "class" in C#

  4. 4

    Using static nested class as generic parameter

  5. 5

    Using super class as the type parameter in generic subtype

  6. 6

    Scala: refer to nested type of class, which comes as type parameter to generic

  7. 7

    Generic class using generic parameter

  8. 8

    C# Break down type parameter in generic method if its a nested class

  9. 9

    c# get the name of the generic type parameter in generic class

  10. 10

    Type Parameter Class with generic java

  11. 11

    Generic class as parameter with inner type

  12. 12

    Generic class as parameter with inner type

  13. 13

    Using generic class as a parameter in method

  14. 14

    Force a generic type parameter to be a class type?

  15. 15

    Avoid passing generic type parameter of class extending generic class

  16. 16

    Avoid passing generic type parameter of class extending generic class

  17. 17

    Using class object as generic type

  18. 18

    How to return a generic type from a nested class using a method in the main class

  19. 19

    Does the name in a parameter type determine if the class is generic?

  20. 20

    Passing parameter to the constructor of a generic Class type

  21. 21

    Chose generic implementation if the type parameter is struct or class

  22. 22

    Generic class E with type parameter to be a subclass of E

  23. 23

    Generic Class:: Type Parameter <T> Property Inheritance

  24. 24

    Passing a class with type parameter as type parameter for generic method in Java

  25. 25

    Use derived class type as parameter of generic action of base class

  26. 26

    Nested type as template parameter of base class

  27. 27

    Construct class using string name for generic parameter

  28. 28

    Generic class with method having generic parameter of different type

  29. 29

    Accessing a class nested in a generic class

HotTag

Archive