-reference(C# 编译器选项)-reference (C# Compiler Options)

-reference 选项使编译器将指定文件中的公共类型信息导入当前项目,从而使你可从指定的程序集文件中引用元数据。The -reference option causes the compiler to import public type information in the specified file into the current project, thus enabling you to reference metadata from the specified assembly files.




包含程序集清单的文件的名称。The name of a file that contains an assembly manifest. 若要导入多个文件,请为每个文件包括一个单独的 -reference 选项。To import more than one file, include a separate -reference option for each file.

一个有效的 C# 标识符,表示将包含程序集中所有命名空间的根命名空间。A valid C# identifier that will represent a root namespace that will contain all namespaces in the assembly.


若要从多个文件导入,请为每个文件包括一个 -reference 选项。To import from more than one file, include a -reference option for each file.

导入的文件必须包含一个清单;输出文件必须已使用-target:module 以外的一个 -target 选项进行了编译。The files you import must contain a manifest; the output file must have been compiled with one of the -target options other than -target:module.

-r 是 -reference 的缩写形式。-r is the short form of -reference.

使用 -addmodule 从不包含程序集清单的输出文件中导入元数据。Use -addmodule to import metadata from an output file that does not contain an assembly manifest.

如果引用的程序集(程序集 A)引用了另一个程序集(程序集 B),那么在下列情况下需要引用程序集 B:If you reference an assembly (Assembly A) that references another assembly (Assembly B), you will need to reference Assembly B if:

  • 使用程序集 A 中的类型,该类型继承自程序集 B 中的类型或实现程序集 B 中的接口。A type you use from Assembly A inherits from a type or implements an interface from Assembly B.

  • 调用具有程序集 B 中的返回类型或参数类型的字段、属性、事件或方法。You invoke a field, property, event, or method that has a return type or parameter type from Assembly B.

使用 -lib 指定一个或多个程序集引用所在的目录。Use -lib to specify the directory in which one or more of your assembly references is located. -Lib 主题还讨论了编译器在哪些目录中搜索程序集。The -lib topic also discusses the directories in which the compiler searches for assemblies.

为使编译器可以识别程序集(而不是模块)中的某个类型,需要强制解析此类型,这可以通过定义此类型的实例来完成。In order for the compiler to recognize a type in an assembly, and not in a module, it needs to be forced to resolve the type, which you can do by defining an instance of the type. 还可通过其他方法为编译器解析程序集中的类型名称:例如,如果从程序集中继承类型,编译器就能识别类型名称。There are other ways to resolve type names in an assembly for the compiler: for example, if you inherit from a type in an assembly, the type name will then be recognized by the compiler.

有时,需要在一个程序集内引用同一组件的两个不同版本。Sometimes it is necessary to reference two different versions of the same component from within one assembly. 为此,请在每个文件的 -reference 开关上使用别名子选项,以区分两个文件。To do this, use the alias suboption on the -reference switch for each file to distinguish between the two files. 此别名将用作组件名的限定符,并解析为其中一个文件中的组件。This alias will be used as a qualifier for the component name, and will resolve to the component in one of the files.

默认情况下使用 csc 响应 (.rsp) 文件,该文件引用常用的 .NET Framework 程序集。The csc response (.rsp) file, which references commonly used .NET Framework assemblies, is used by default. 如果希望编译器不要使用 csc.rsp,请使用 -noconfigUse -noconfig if you do not want the compiler to use csc.rsp.


在 Visual Studio 中,请使用“添加引用”对话框。In Visual Studio, use the Add Reference dialog box. 有关详细信息,请参阅如何:使用引用管理器添加或删除引用For more information, see How to: Add or Remove References By Using the Reference Manager. 若要确保通过使用 -reference 添加引用与通过使用“添加引用”对话框添加引用之间的行为等效,请将要添加的程序集的“嵌入互操作类型”属性设置为“False”。To ensure equivalent behavior between adding references by using -reference and adding references by using the Add Reference dialog box, set the Embed Interop Types property to False for the assembly that you're adding. “True”是该属性的默认值。True is the default value for the property.


本示例演示如何使用外部别名功能。This example shows how to use the extern alias feature.

编译源文件,并从先前已编译的 grid.dllgrid20.dll 中导入元数据。You compile the source file and import metadata from grid.dll and grid20.dll, which have been compiled previously. 这两个 DLL 包含同一组件的不同版本,将使用两个带有别名选项的 -reference 编译源文件。The two DLLs contain separate versions of the same component, and you use two -reference with alias options to compile the source file. 这两个选项如下所示:The options look like this:

-reference:GridV1=grid.dll -reference:GridV2=grid20.dll  

这将设置外部别名 GridV1GridV2,将通过 extern 语句在程序中使用它们:This sets up the external aliases GridV1 and GridV2, which you use in your program by means of an extern statement:

extern alias GridV1;  
extern alias GridV2;  
// Using statements go here.  

完成此操作后,可以通过在控件名称前添加 GridV1 前缀来引用 grid.dll 中的网格控件,如下所示:Once this is done, you can refer to the grid control from grid.dll by prefixing the control name with GridV1, like this:


此外,可以通过在控件名称前添加 GridV2 前缀来引用 grid20.dll 中的网格控件,如下所示:In addition, you can refer to the grid control from grid20.dll by prefixing the control name with GridV2 like this:


请参阅See also