全局导入/在 .NET 中使用别名
在一个文件/类中使用导入别名,我们可以通过分配我们自己的自定义别名来引用类库名称空间,如下所示:
' VB
Imports Db = Company.Lib.Data.Objects
// C#
using Db = Company.Lib.Data.Objects;
然后我们可以通过以下方式引用Company.Lib.Data.Objects内部的类:使用我们分配的 Db
别名。
是否可以在全局级别执行此操作,以便将别名应用于整个解决方案而不是仅应用于一个文件/类?
目前,我们正在使用 Web 应用程序,因此我希望我们可以在 web.config 中添加一些内容,但我也对 Windows 窗体、控制台应用程序和/或类库是否可以实现这一点感兴趣。
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;
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是 VB.Net 项目中支持的场景。执行此操作的方法如下:
”将为项目中的所有文件设置别名。
然而,这在 C# 项目中不起作用。 C# 作为一种语言没有全局使用/导入的概念。相反,它仅在文件级别支持它们。
Yes this is a supported scenario in VB.Net projects. The way to do this is the following
This will set up the alias for all files in the project.
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.
在您网站的 web.config 文件中 - 或者很可能是项目的 app.config 文件(未确认)中,
您在此部分中放置的任何内容都应该是代码隐藏页面顶部的 Imports 子句的有效替换。为我创造了奇迹,请告诉我是否对您有帮助
In your web.config file for your website - or most likely app.config file for a project (not confirmed)
anything that you put in this section should be a valid replacement for the Imports clause on the top of your code behind pages. Worked wonders for me, let me know if it helps you out