C# Visual Studio 8 中程序集引用的别名属性有什么用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/286632/
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-03 21:42:50  来源:igfitidea点击:

What use is the Aliases property of assembly references in Visual Studio 8

c#visual-studioreferencecsproj

提问by Spencer Booth

When I add an assembly reference to a project in Visual Studio 8 the Aliases property, of that reference, is set to "global". What is this property good for and why is it set to global?

当我向 Visual Studio 8 中的项目添加程序集引用时,该引用的别名属性设置为“全局”。这个属性有什么用,为什么它被设置为全局?

MSDN tells me that this is a list of aliases for the assembly but not why I might want to use this property or why most are aliased as "global".

MSDN 告诉我这是程序集的别名列表,但不是为什么我可能想要使用此属性或为什么大多数别名为“全局”。

MSDN reference

MSDN参考

采纳答案by Jon Skeet

This is for "extern aliases". Suppose you want to use two different types, both of which are called Foo.Bar(i.e. Barin a namespace of Foo). The two types will be in different assemblies (by definition) - you use the property in VS to associate an alias with each reference, then you can do:

这是用于“外部别名”。假设您想使用两种不同的类型,这两种类型都被调用Foo.Bar(即Bar在 的命名空间中Foo)。这两种类型将在不同的程序集中(根据定义)-您使用 VS 中的属性将别名与每个引用关联,然后您可以执行以下操作:

extern alias FirstAlias;
extern alias SecondAlias;

using FirstBar = FirstAlias::Foo.Bar;
using SecondBar = SecondAlias::Foo.Bar;

and then use FirstBarand SecondBarin your code.

然后在您的代码中使用FirstBarSecondBar

So basically it's an extra level of naming - and you shouldn't use it unless you really, really have to. It will confuse a lot of people. Try to avoid getting into that situation in the first place - but be aware of this solution for those times where you just can't avoid it.

所以基本上它是一个额外的命名级别 - 除非你真的,真的必须使用它,否则你不应该使用它。会迷惑很多人。首先尽量避免陷入这种情况 - 但在无法避免的情况下,请注意此解决方案。

回答by Marc Gravell

Search for "extern alias"; it is a very rarely used feature that is only needed to disambiguate between two dlls that contribute the same types (for example, two different versions of the same assembly, or two assemblies that have a class which shares a fully-qualified-name).

搜索“外部别名”;这是一个很少使用的功能,只需要在提供相同类型的两个 dll 之间消除歧义(例如,同一程序集的两个不同版本,或具有共享完全限定名称的类的两个程序集)。

"global" is the default. For example, if you have a class called Foo.System, you can unambiguously refer to the main Systemnamespace via global::System.

“全局”是默认值。例如,如果您有一个名为 的类Foo.System,则可以通过 明确地引用主System命名空间global::System