C# Anonymous Type和一般的Named Type并没有本质的区别。但是话不能太绝对,他们之间还是有一点小小的差异。到底是什么样差异,我在这里先卖一个关子。在具体介绍这个差异的时候,我们先来看看一个Sample:
Artech.NewFeatureInCSharp.Library中定一个Employee Type:
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- namespaceArtech.NewFeatureInCSharp.Library
- {
- publicclassEmployee
- {
- privateGuid_id;
- privatestring_name;
- publicGuidID
- {
- get{return_id;}
- set{_id=value;}
- }
- publicstringName
- {
- get{return_name;}
- set{_name=value;}
- }
- }
- }
和一个Static的Utility Class:
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- namespaceArtech.NewFeatureInCSharp.Library
- {
- publicstaticclassUtility
- {
- publicstaticobjectAnonymous_GetEmployee(Guidid,stringname)
- {
- returnnew{ID=id,Name=name};
- }
- publicstaticEmployeeGetEmployee(Guidid,stringname)
- {
- returnnewEmployee{ID=id,Name=name};
- }
- }
- }
在Utility中定义了两个GetEmployee方法,分别返回以Anonymous Type形式和Named Type形式的Employee对象。
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingArtech.NewFeatureInCSharp.Library;
- namespaceArtech.NewFeatureInCSharp.ConsoleApp
- {
- classProgram
- {
- staticvoidMain(string[]args)
- {
- varv1=new{ID=Guid.NewGuid(),Name="ZhangSan"};
- varv2=new{ID=Guid.NewGuid(),Name="LiSi"};
- varv3=Utility.Anonymous_GetEmployee(Guid.NewGuid(),"WangWu");
- Console.WriteLine("varv1=new{ID=Guid.NewGuid(),Name=\"ZhangSan\"};");
- Console.WriteLine("varv2=new{ID=Guid.NewGuid(),Name=\"LiSi\"};");
- Console.WriteLine("varv3=Utility.Anonymous_GetEmployee(Guid.NewGuid(),\"WangWu\");");
- Console.WriteLine("\nv1.GetType()={0}",v1.GetType());
- Console.WriteLine("v2.GetType()={0}",v2.GetType());
- Console.WriteLine("v3.GetType()={0}",v3.GetType());
- Console.WriteLine("\nobject.ReferenceEquals(v1.GetType(),v2.GetType())={0}",
object.ReferenceEquals(v1.GetType(),v2.GetType()));- Console.WriteLine("object.ReferenceEquals(v1.GetType(),v3.GetType())={0}",
object.ReferenceEquals(v1.GetType(),v3.GetType()));- Console.WriteLine("\n\n");
- varv4=newEmployee{ID=Guid.NewGuid(),Name="ZhangSan"};
- varv5=newEmployee{ID=Guid.NewGuid(),Name="LiSi"};
- varv6=Utility.GetEmployee(Guid.NewGuid(),"WangWu");
- Console.WriteLine("varv4=newEmployee{ID=Guid.NewGuid(),Name=\"ZhangSan\"};");
- Console.WriteLine("varv5=newEmployee{ID=Guid.NewGuid(),Name=\"LiSi\"};");
- Console.WriteLine("varv6=Utility.GetEmployee(Guid.NewGuid(),\"WangWu\");");
- Console.WriteLine("\nv4.GetType()={0}",v4.GetType());
- Console.WriteLine("v5.GetType()={0}",v5.GetType());
- Console.WriteLine("v6.GetType()={0}",v6.GetType());
- Console.WriteLine("\nobject.ReferenceEquals(v4.GetType(),v5.GetType())={0}",
object.ReferenceEquals(v4.GetType(),v5.GetType()));- Console.WriteLine("object.ReferenceEquals(v4.GetType(),v6.GetType())={0}",
object.ReferenceEquals(v4.GetType(),v6.GetType()));- }
- }
- }
代码不复杂,我在这里简单介绍一下整体的结构。这个结构分两部分,第一部分是基于C# Anonymous Type的,另一部分是基于Named Employee Type的。在第一部分中,我首先创建了3个C# Anonymous Type的Instance:v1、v2和v3(v3是通过调用定义在Artech.NewFeatureInCSharp.Library中的Utility获得,其余两个则直接通过Inline的方式创建),第二部分也具有相同的代码结构。
【编辑推荐】