如何控制.Net在名称空间冲突中选择哪一个程序集?

时间:2022-10-27 07:38:59

I need to (bear with me) for some reason or another use the WinRT version of the System.Text.Encoding namespace. I can add a reference to the assembly manually and such, but it will still use mscorlib's implementation. And I can't completely remove mscorlib apparently.

由于某种原因,我需要使用System.Text的WinRT版本。编码名称空间。我可以手动向程序集添加一个引用,但是它仍然使用mscorlib的实现。显然我不能完全移除mscorlib。

How can I force my project to use WinRT's System.Text.Encoding.dll instead of mscorlib?

如何强制我的项目使用WinRT的System.Text.Encoding。dll而不是mscorlib ?

Basically, I need it to generate this piece of IL:

基本上,我需要它来生成这个IL:

call class [System.Text.Encoding]System.Text.Encoding [System.Text.Encoding]System.Text.Encoding::get_UTF8()

instead of this:

而不是:

call class [mscorlib]System.Text.Encoding [mscorlib]System.Text.Encoding::get_UTF8()

1 个解决方案

#1


7  

You need to alias the reference. To do this, go to Solution Explorer and select the reference you want to alias. View the Properties and edit the Aliases field to add a unique alias name.

您需要为引用添加别名。为此,转到解决方案资源管理器并选择要别名的引用。查看属性并编辑Aliases字段以添加惟一的别名。

如何控制.Net在名称空间冲突中选择哪一个程序集?

Once you've defined a unique alias, you edit your code to add the extern alias declaration.

一旦定义了唯一的别名,就可以编辑代码以添加extern别名声明。

extern alias myalias;

Finally, you reference the types via the alias as in (this example aliased System.dll):

最后,您可以通过别名(这个示例别名System.dll)引用这些类型:

myalias::System.Diagnostics.Trace.WriteLine("I referenced this via my alias.");

This now targets the exact reference you want, even if other references also provide a type with the same name and namespace.

现在,它的目标是您想要的确切引用,即使其他引用也提供具有相同名称和名称空间的类型。

For additional info on aliases, see this * answer on What use is the Aliases property of assembly references.

有关别名的更多信息,请参见关于使用什么是程序集引用的别名属性的*答案。

#1


7  

You need to alias the reference. To do this, go to Solution Explorer and select the reference you want to alias. View the Properties and edit the Aliases field to add a unique alias name.

您需要为引用添加别名。为此,转到解决方案资源管理器并选择要别名的引用。查看属性并编辑Aliases字段以添加惟一的别名。

如何控制.Net在名称空间冲突中选择哪一个程序集?

Once you've defined a unique alias, you edit your code to add the extern alias declaration.

一旦定义了唯一的别名,就可以编辑代码以添加extern别名声明。

extern alias myalias;

Finally, you reference the types via the alias as in (this example aliased System.dll):

最后,您可以通过别名(这个示例别名System.dll)引用这些类型:

myalias::System.Diagnostics.Trace.WriteLine("I referenced this via my alias.");

This now targets the exact reference you want, even if other references also provide a type with the same name and namespace.

现在,它的目标是您想要的确切引用,即使其他引用也提供具有相同名称和名称空间的类型。

For additional info on aliases, see this * answer on What use is the Aliases property of assembly references.

有关别名的更多信息,请参见关于使用什么是程序集引用的别名属性的*答案。