在c#中,extern alias有何作用?

时间:2022-09-02 11:11:46

To resolve a namespace conflict between assemblies in C#, I gave one an Alias of NLog:

为了解决c#中程序集之间的名称空间冲突,我给了一个NLog别名:

在c#中,extern alias有何作用?

Now, I can refer to a namespace within that assembly like so:

现在,我可以在这样的程序集中引用一个名称空间:

public class Logger : NLog::NLog.LogReceiverService
{
   // ...
}

However, this won't compile until I use the extern alias keyword at the top of my file:

但是,直到我在文件的顶部使用外部别名关键字:

extern alias NLog;

My Question:

我的问题:

What purpose does the extern alias keyword serve? Shouldn't NLog::NLog.LogReceiverService be sufficient to fully disambiguate the assembly alias, namespace and type I'm referring to?

外部别名关键字服务的目的是什么?不应该NLog::NLog。LogReceiverService是否足以完全消除我所指的程序集别名、名称空间和类型的歧义?

1 个解决方案

#1


4  

Setting the Aliases property on the reference will only designate it as that alias while the initial load of assembly references is done for your project as run time. In order to use that aliased assembly reference in your file, you must specify that you are going to use it via the extern alias keyword, which sets the compiler directives to look for that alias.

在引用上设置Aliases属性只会将其指定为该别名,而程序集引用的初始加载作为运行时完成。为了在文件中使用别名程序集引用,您必须指定您将通过extern alias关键字使用它,该关键字设置了编译器指令以查找该别名。

You would think that they would have some type of global aliasing, but I am thinking they force the explicit alias use due to a performance hit that is incurred on the alias.

您可能会认为它们会有某种类型的全局混叠,但是我认为它们会强制使用由于在别名上发生的性能损失而使用的显式别名。

Here is the reference material on extern alias: http://msdn.microsoft.com/en-us/library/ms173212.aspx

以下是关于extern alias的参考资料:http://msdn.microsoft.com/en-us/library/ms173212.aspx

#1


4  

Setting the Aliases property on the reference will only designate it as that alias while the initial load of assembly references is done for your project as run time. In order to use that aliased assembly reference in your file, you must specify that you are going to use it via the extern alias keyword, which sets the compiler directives to look for that alias.

在引用上设置Aliases属性只会将其指定为该别名,而程序集引用的初始加载作为运行时完成。为了在文件中使用别名程序集引用,您必须指定您将通过extern alias关键字使用它,该关键字设置了编译器指令以查找该别名。

You would think that they would have some type of global aliasing, but I am thinking they force the explicit alias use due to a performance hit that is incurred on the alias.

您可能会认为它们会有某种类型的全局混叠,但是我认为它们会强制使用由于在别名上发生的性能损失而使用的显式别名。

Here is the reference material on extern alias: http://msdn.microsoft.com/en-us/library/ms173212.aspx

以下是关于extern alias的参考资料:http://msdn.microsoft.com/en-us/library/ms173212.aspx