介绍C# Anonymous Type

开发 后端
这里介绍C# Anonymous Type,C# Anonymous Type仅仅是C# 3.0的新的特性,而没有说Anonymous Type是.NET Framework 3.5的新特性。

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来说他们之间没有什么本质的区别。

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

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

< >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。

【编辑推荐】

  1. 浅析C# ArrayList
  2. C#对象初始化学习总结
  3. 使用C#正则表达式匹配相关字符串
  4. C#改写方法学习笔记
  5. 概述C#加框和消框
责任编辑:佚名 来源: 博客园
相关推荐

2009-08-18 09:24:52

C# Anonymou

2009-08-17 16:32:34

C# Anonymou

2009-08-12 18:35:36

C# ArrayLis

2009-07-31 14:15:38

C# 构造函数

2009-08-04 08:48:44

C#内置特性

2009-08-12 09:41:28

C# Director

2009-08-18 16:45:40

C# Raw Sock

2009-09-03 15:57:11

C# SystemMe

2009-08-12 15:43:02

操作C# Datase

2009-08-12 15:34:40

C# DBNull

2009-08-25 10:24:29

C# delegate

2009-08-10 16:30:56

C# BitmapDa

2009-09-02 17:20:50

C# Parsing

2009-08-21 17:55:52

C#复合控件

2009-08-24 15:41:50

C#连接SQL Ser

2009-09-03 17:21:51

C# VSProjec

2009-09-03 16:51:27

C#类属性

2009-09-01 10:20:28

C#多种语句

2009-08-10 16:19:37

C#冒泡排序

2009-08-14 17:27:56

C#方法参数
点赞
收藏

51CTO技术栈公众号