Reputation: 5769
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.
Upvotes: 3
Views: 1616
Reputation: 836
I think your main goal will probably be achieved simply by putting the sub class next to the parent in the same namespace.
The namespace is really what should be organising the code in the way you are talking about in the comments under your post.
You should really only be defining a class as a sub class it only the parent class is going to be using it.
Upvotes: 0
Reputation: 43886
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
Empty
(so doesn't know that no instance of Generic<>.Nested
is created)IEnumerabe<Generic<>.Nested>
, which would be a type with "unsatisfied type parameters", tooUpvotes: 3