全局导入/在 .NET 中使用别名

发布于 2024-08-26 01:34:11 字数 399 浏览 2 评论 0原文

在一个文件/类中使用导入别名,我们可以通过分配我们自己的自定义别名来引用类库名称空间,如下所示:

' 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

折戟 2024-09-02 01:34:11

是的,这是 VB.Net 项目中支持的场景。执行此操作的方法如下:

  • 右键单击​​“解决方案资源管理器”中的项目,然后选择“属性”
  • 转到“引用”选项卡
  • 在“导入的命名空间”字段中,输入“Db=Company.Lib.Data.Objects”
  • 点击“添加用户导入

”将为项目中的所有文件设置别名。

然而,这在 C# 项目中不起作用。 C# 作为一种语言没有全局使用/导入的概念。相反,它仅在文件级别支持它们。

Yes this is a supported scenario in VB.Net projects. The way to do this is the following

  • Right Click on the project in Solution Explorer and select Properties
  • Go to the References tab
  • In the "Imported Namespaces" field type "Db=Company.Lib.Data.Objects"
  • Hit "Add User Import"

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.

自由范儿 2024-09-02 01:34:11

在您网站的 web.config 文件中 - 或者很可能是项目的 app.config 文件(未确认)中,

    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
        <namespaces>
            <clear/>
            <add namespace="System"/>
            <add namespace="System.Collections"/>
            <add namespace="System.Collections.Specialized"/>
            <add namespace="System.Configuration"/>

您在此部分中放置的任何内容都应该是代码隐藏页面顶部的 Imports 子句的有效替换。为我创造了奇迹,请告诉我是否对您有帮助

In your web.config file for your website - or most likely app.config file for a project (not confirmed)

    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
        <namespaces>
            <clear/>
            <add namespace="System"/>
            <add namespace="System.Collections"/>
            <add namespace="System.Collections.Specialized"/>
            <add namespace="System.Configuration"/>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文