外部别名(C# 参考)extern alias (C# Reference)

有时你可能不得不引用具有相同的完全限定类型名称的程序集的两个版本。You might have to reference two versions of assemblies that have the same fully-qualified type names. 例如,可能需要在同一应用程序中使用某程序集的两个或多个版本。For example, you might have to use two or more versions of an assembly in the same application. 通过使用外部程序集别名,可在别名命名的根级别命名空间内包装每个程序集的命名空间,使其能够在同一文件中使用。By using an external assembly alias, the namespaces from each assembly can be wrapped inside root-level namespaces named by the alias, which enables them to be used in the same file.

备注

外部关键字还被用作方法修饰符,用于声明在非托管代码中编写的方法。The extern keyword is also used as a method modifier, declaring a method written in unmanaged code.

若要引用具有相同的完全限定类型名称的两个程序集,必须在命令提示符处指定别名,如下所示:To reference two assemblies with the same fully-qualified type names, an alias must be specified at a command prompt, as follows:

/r:GridV1=grid.dll

/r:GridV2=grid20.dll

这将创建外部别名 GridV1GridV2This creates the external aliases GridV1 and GridV2. 若要从程序中使用这些别名,请通过使用 extern 关键字引用它们。To use these aliases from within a program, reference them by using the extern keyword. 例如:For example:

extern alias GridV1;

extern alias GridV2;

每个外部别名声明都会引入与全局命名空间并行(但不位于其中)的额外根级别命名空间。Each extern alias declaration introduces an additional root-level namespace that parallels (but does not lie within) the global namespace. 因此,可以使用其完全限定的名称(根植于相应的命名空间别名中)无歧义地引用每个程序集的类型。Thus types from each assembly can be referred to without ambiguity by using their fully qualified name, rooted in the appropriate namespace-alias.

在上一示例中,GridV1::Gridgrid.dll 中的网格控件,GridV2::Gridgrid20.dll 中的网格控件。In the previous example, GridV1::Grid would be the grid control from grid.dll, and GridV2::Grid would be the grid control from grid20.dll.

C# 语言规范C# Language Specification

有关详细信息,请参阅 C# 语言规范For more information, see the C# Language Specification. 该语言规范是 C# 语法和用法的权威资料。The language specification is the definitive source for C# syntax and usage.

请参阅See Also

C# 参考C# Reference
C# 编程指南C# Programming Guide
C# 关键字C# Keywords
命名空间关键字Namespace Keywords
:: 运算符:: Operator
/reference(C# 编译器选项)/reference (C# Compiler Options)