开发者

What are the strange property types generated in a C# anonymous type?

Observe the following trivial anonymous type in C#

new { X = 5 };

The respective compiler generated code as seen in Reflector (omitting the object method overrides) is:

[CompilerGenerated]
internal sealed class <>f__AnonymousType0<<X>j__TPar>
{
    // Fields
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private readonly <X>j__TPar <X>i__Field;

    // Methods
    [DebuggerHidden]
    public <>f__AnonymousType0(<X>j__TPar X)
    {
        this.<X>i__Field = X;
开发者_运维知识库    }

    // Properties
    public <X>j__TPar X
    {
        get
        {
            return this.<X>i__Field;
        }
    }
}

My question is WOE (What On Earth) is <X>j__TPar? The type is reflected nowhere in Reflector (pun intended).


<X>j__TPar is the name of the generic argument used in the <>f__AnonymousType0 type definition. A bit less readable indeed than T but guaranteed to never collide with another name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜