成都网站建设设计

将想法与焦点和您一起共享

介绍C#AnonymousType

C# Anonymous Type仅仅是C# 3.0的新的特性,而没有说Anonymous Type是.NET Framework 3.5的新特性。这是因为Anonymous Type仅仅是.NET Programming Language和相应的Compiler的新引入的特征。而对于.NET Framework 3.5来说,它看不到这和原来有什么不同,换句话说,对于Anonymous Type和一般的Named Type,对于CLR来说他们之间没有什么本质的区别。

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名与空间、网站空间、营销软件、网站建设、长乐网站维护、网站推广。

对于下面这样的一段简单的代码:

 
 
 
 
  1. public sealed class <>f__AnonymousType0<<>j__AnonymousTypeTypeParameter1, 
    <>j__AnonymousTypeTypeParameter2>
  2. {
  3. // Properties
  4. public <>j__AnonymousTypeTypeParameter1ID{ get; set; }
  5. public j__AnonymousTypeTypeParameter2 Name{ get; set; }
  6. // Fields
  7. private j__AnonymousTypeTypeParameter1 <>i__AnonymousTypeField3;
  8. private j__AnonymousTypeTypeParameter2 <>i__AnonymousTypeField4;
  9. }

< >j__Anonymous Type Type Parameter1 和< >j__Anonymous Type Type Parameter2这两个Generic Type代表我在 {} 中制定ID和Name的类型。通过这个结构,我们发现其定义和一般的Generic Type并没有什么区别。

为了进一步了解生成什么样的C# Anonymous Type,我们使用IL DASM在IL级别看看生成的Anonymous Type的大体结构:

为了做一个对比,下面是我们最开始定义的Named Employee Type在IL DASM中的结构:

如果想更清楚了解C# Anonymous Type的本质,建议读者亲自使用IL DASM看看的每个成员具体的IL。


网站标题:介绍C#AnonymousType
本文URL:https://chengdu.cdxwcx.cn/article/cdscjec.html