为什么在编译C#文件的时候不用加/r:System.dll 类库的引用

 
类集与名称空间的关系
.NET文档中对两者关系的描述是:“名称空间在概念上与类集成正交关系”。用更通俗的话说,它们之间没有任何直接联系。一个名称空间中的类可能分布在几个类集当中,一个类集也可能包含几个不同的名称空间中类的实现。系统类库中确实存在很多同名的类集和名称空间,但那只能说是命名上的巧合罢了。
下表列出了常用的名称空间以及它们的实现类集之间的对应关系:
   
名称空间
对应的类集
System
mscorlib, System
System.IO
mscorlib, System
System.Xml
System.Data, System.Xml
System.Data
System.Data
System.Net
System
System.Reflection
mscorlib,
System.Security
mscorlib,
System.InteropServices
mscorlib,
System.Runtime.Remoting
mscorlib,
System.Runtime.Serialization
mscorlib,
 
C#编译器允许使用配置文件来简化编译参数的设置,例如将如下内容保存到C#配置文件MyExe.rsp:
   #注释:编译source1.cs source2.cs,生成MyExe.exe
/target:exe /out:MyExe.exe source1.cs source2.cs
然后就可以通过制定配置文件的方式来编译MyExe程序了:
csc @MyExe.rsp
 
需要引用的类集也是配置文件的内容之一,可以在配置文件中指定程序需要引用得类集。鉴于一些类集在编写程序的过程中比较常用,C#的设计者将这些类集的引用放在了C#编译器的缺省配置文件当中,该配置文件位于%SystemRoot%/Microsoft.Net/Framework/%version%/csc.rsp,其中%SystemRoot%是Windows的安装目录,而%version%是.NET Framework的版本号。该文件的内容如下:
# This file contains command-line options that the C#
# command line compiler (CSC) will process as part
# of every compilation, unless the "/noconfig" option
# is specified.
 
# Reference the common Framework libraries
/r:Accessibility.dll
/r:Microsoft.Vsa.dll
/r:System.Configuration.Install.dll
/r:System.Data.dll
/r:System.Design.dll
/r:System.DirectoryServices.dll
/r:System.dll
/r:System.Drawing.Design.dll
/r:System.Drawing.dll
/r:System.EnterpriseServices.dll
/r:System.Management.dll
/r:System.Messaging.dll
/r:System.Runtime.Remoting.dll
/r:System.Runtime.Serialization.Formatters.Soap.dll
/r:System.Security.dll
/r:System.ServiceProcess.dll
/r:System.Web.dll
/r:System.Web.RegularExpressions.dll
/r:System.Web.Services.dll
/r:System.Windows.Forms.Dll
/r:System.XML.dll
正是由于编译器的配置文件中添加对常用类库的引用(除非通过使用/noconfig显式指定忽略配置文件)所以在编译的时候不需要显示引用哪个类库文件,编译器会自动根据配置文件添加对类库的引用。
 
posted @ 2013-03-25 17:55  Predator  阅读(591)  评论(0编辑  收藏  举报