Scenario is very rare, but quite simple: you define a generic class, then create a nested class which inherits from outer class and define a associative field (of self type) within nested. Code snippet is simpler, than description:
class Outer<T>
{
class Inner : Outer<Inner>
{
Inner field;
}
}
after decompilation of IL, C# code look like this:
internal class Outer<T>
{
private class Inner : Outer<Outer<T>.Inner>
{
private Outer<Outer<T>.Inner>.Inner field;
}
}
This seems to be fair enough, but when you change the type declaration of the field, things become trickier. So when I change the field declaration to
Inner.Inner field;
After decompilation this field will looks like this:
private Outer<Outer<Outer<T>.Inner>.Inner>.Inner field;
I understand, that class 'nestedness' and inheritance don't quite get along with each other, but why do we observe such behavior? Is the Inner.Inner
type declaration has changed the type at all? Are Inner.Inner
and Inner
types differ in some way in this context?
When things become very tricky
You can see the decompiled source code for the class below. It's really huge and has total length of 12159 symbols.
class X<A, B, C>
{
class Y : X<Y, Y, Y>
{
Y.Y.Y.Y.Y.Y y;
}
}
Finally, this class:
class X<A, B, C, D, E>
{
class Y : X<Y, Y, Y, Y, Y>
{
Y.Y.Y.Y.Y.Y.Y.Y.Y y;
}
}
results in 27.9 MB (29,302,272 bytes)
assembly and Total build time: 00:43.619
Tools used
Compilation is done under C# 5 and C# 4 compilers. Decompilation is done by dotPeek. Build configurations: Release
and Debug
question from:
https://stackoverflow.com/questions/14177225/why-does-field-declaration-with-duplicated-nested-type-in-generic-class-results