问题描述
在一个文件/类中使用导入别名,我们可以通过分配我们自己的自定义别名来引用类库命名空间,如下所示:
Using import aliasing in one file/class, we can reference class library namespaces by assigning our own custom alias like this:
' VB
Imports Db = Company.Lib.Data.Objects
// C#
using Db = Company.Lib.Data.Objects;
然后我们可以使用我们分配的 Db
别名来引用 Company.Lib.Data.Objects
中的类.
And then we are able to reference the classes inside of Company.Lib.Data.Objects
by using the Db
alias that we assigned.
是否可以在全局级别执行此操作,以便将别名应用于整个解决方案而不仅仅是一个文件/类?
Is it possible to do this at the global level so that the alias is applied to the entire solution instead of just one file/class?
目前,我们正在使用 Web 应用程序,所以我希望我们可以在 web.config 中添加一些内容,但我也对 Windows 窗体、控制台应用程序和/或类库是否可以实现这一点感兴趣.
Currently, we are working with web applications, so I was hoping we could add something to web.config, but I am also interested in whether or not this is possible with windows forms, console apps, and/or class libraries.
推荐答案
是的,这是 VB.Net 项目中支持的方案.这样做的方法如下
Yes this is a supported scenario in VB.Net projects. The way to do this is the following
- 在解决方案资源管理器中右键单击项目并选择属性
- 转到参考"标签
- 在导入的命名空间"字段中输入Db=Company.Lib.Data.Objects"
- 点击添加用户导入"
这将为项目中的所有文件设置别名.
This will set up the alias for all files in the project.
但这在 C# 项目中不起作用.C# 作为一种语言没有全局使用/导入的概念.相反,它仅在文件级别支持它们.
This however does not work in C# projects. C# as a language doesn't have the concept of a global using/import. Instead it only supports them at the file level.
这篇关于.NET 中的全局导入/使用别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!