我们说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;
然后我们再看看IL:
我们再这里就可以看出Compiler将会为p1这个Anonymous Type创建一个名为<>f__AnonymousType0`2<string,int32>的类型。参数将根据数据成员的具体结构,这里也是声明一个类型。这个类继承自object,并且重写了ToString() 和 GetHashCode()的实现。所有的匿名类型自动继承自System.Object ,并且重写了Equals(), GetHashCode(), and ToString(). 如下图:
c#3.0系列:Anonymous Type In CLR(3.5)
结构如下:
c#3.0系列:Anonymous Type In CLR(3.5)internal sealed class <>f__AnonymousType0<<Name>j__TPar, <Age>j__TPar>
c#3.0系列:Anonymous Type In CLR(3.5){
c#3.0系列:Anonymous Type In CLR(3.5)    
// Fields    
c#3.0系列:Anonymous Type In CLR(3.5)
private readonly <Age>j__TPar <Age>i__Field;
c#3.0系列:Anonymous Type In CLR(3.5)        
private readonly <Name>j__TPar <Name>i__Field;
c#3.0系列:Anonymous Type In CLR(3.5)
c#3.0系列:Anonymous Type In CLR(3.5)    
// Methods    
c#3.0系列:Anonymous Type In CLR(3.5)
public <>f__AnonymousType0(<Name>j__TPar Name, <Age>j__TPar Age);
c#3.0系列:Anonymous Type In CLR(3.5)    
c#3.0系列:Anonymous Type In CLR(3.5)    
public override bool Equals(object value);
c#3.0系列:Anonymous Type In CLR(3.5)        
public override int GetHashCode();
c#3.0系列:Anonymous Type In CLR(3.5)        
public override string ToString();
c#3.0系列:Anonymous Type In CLR(3.5)
c#3.0系列:Anonymous Type In CLR(3.5)    
// Properties
c#3.0系列:Anonymous Type In CLR(3.5)
    public <Age>j__TPar Age { get; }
c#3.0系列:Anonymous Type In CLR(3.5)    
public <Name>j__TPar Name { get; }
c#3.0系列:Anonymous Type In CLR(3.5)}
c#3.0系列:Anonymous Type In CLR(3.5)
再看看下面的例子。
我们看看输出的结果。
c#3.0系列:Anonymous Type In CLR(3.5)
通过以上的结果我们更能对Compiler会为Anonymous Type创建的类型更好的理解。
GetHashCode()   实现时,根据匿名类型的每个成员作为System.Collections.Generic.EqualityComparer<T>的输入,来计算hash值。因此,如果2个匿名类型有相同的属性字段,同时每个字段的值也相同,那么生成的Hash值也就相等。
ToString()   只是简单的用 属性名称/属性值 构造了一个字符串。
Equals()      结果相等,因为它使用值来比较相等。
==             结果不等,因为匿名类型没有重载 == 和 != , 这两个操作符默认情况下,比较的是对象的引用而不是值。
总结:
• Anonymous types 继承 System.Object.
• The fields and properties 是read-only.
• Anonymous types 不支持events, custom methods, custom operators, or custom overrides.
• Anonymous types 是个密封类.
• Anonymous types 用自己定义的构造方法(不可以修改)来创建的.
最重要的是
Compiler在生成Anonymous types 的时候,并不是为每个如{N=?, N2 =? , ...}的结构生成一个不同的Type,它只会为不同的参数列表的结构:参数的名称,参数的数据类型,参数的相互顺序定义不同的Type(不包含参数的值)。而具有相同的参数列表的{N=?, N2 =? , ...}会共享同一个Type。但是这种仅限于在同一个Assembly中,编译器只会生成一个匿名类型,这些对象都是该唯一的匿名类型的实例。
在同一个程序集内,两个匿名对象具有相同的属性、相同的属性顺序。编译器将认为这两个匿名对象是相同的。

相关文章: