C# 如何从特定程序集中引用命名空间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/517058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 06:27:30  来源:igfitidea点击:

How to reference a namespace from a specific assembly?

c#.netassemblies

提问by George Mauer

So here is my problem.

所以这是我的问题。

  • My (test) project references both Castle Windsor and Rhino Mocks.
  • I am creating a class which implements Castle.Core.Interceptor.IInterceptor from the Castle.Core.dll assembly
  • In building Rhino Mocks, Ayende used Castle.Core.Interceptor and includes the whole darn namespace inside the Rhino.Mocks.dll
  • 我的(测试)项目同时引用了 Castle Windsor 和 Rhino Mocks。
  • 我正在创建一个从 Castle.Core.dll 程序集实现 Castle.Core.Interceptor.IInterceptor 的类
  • 在构建 Rhino Mocks 时,Ayende 使用了 Castle.Core.Interceptor 并将整个 darn 命名空间包含在 Rhino.Mocks.dll 中

So when I try to build, I get the error

所以当我尝试构建时,我收到错误

The type 'Castle.Core.Interceptor.IInterceptor' exists in both 'c:...\Libraries\Rhino.Mocks.dll' and 'c:...\Libraries\Castle.Core.dll'

'Castle.Core.Interceptor.IInterceptor' 类型同时存在于 'c:...\Libraries\Rhino.Mocks.dll' 和 'c:...\Libraries\Castle.Core.dll'

How then do I specify that I want to use the IInterceptor instance from the Castle.Core.dll rather than the one included in Rhino Mocks?

那么我如何指定我想使用 Castle.Core.dll 中的 IInterceptor 实例而不是包含在 Rhino Mocks 中的实例?

采纳答案by George Mauer

Let's throw the specific answer up here in case someone comes along later. From article here.

让我们把具体的答案放在这里,以防以后有人来。从这里的文章。

  • Select one of the two assemblies under project references (in my case I selected Castle.Core). Hit F4 to bring up properties and enter alias CastleCore
  • At the top of the problematic cs file put extern alias CastleCore;
  • Reference your class with CastleCore::Castle.Core.Interceptors.IInterceptor. Or in my case I simply did:
  • 选择项目引用下的两个程序集之一(在我的例子中我选择了 Castle.Core)。按 F4 调出属性并输入别名 CastleCore
  • 在有问题的 cs 文件的顶部放置 extern alias CastleCore;
  • CastleCore::Castle.Core.Interceptors.IInterceptor. 或者就我而言,我只是做了:

using cci = CastleCore::Castle.Core.Interceptors;

使用 cci = CastleCore::Castle.Core.Interceptors;

and can now reference

现在可以参考

cci.IInterceptor

回答by Andrew Hare

You can use an extern aliasto alias one of the assemblies to prevent the ambiguity.

您可以使用extern alias为其中一个程序集设置别名以防止歧义。