C# 是否支持像 VB.NET 一样的项目范围的默认命名空间导入? [英] Does C# Support Project-Wide Default Namespace Imports Like VB.NET?

查看:15
本文介绍了C# 是否支持像 VB.NET 一样的项目范围的默认命名空间导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是最近转换为 C# 的 VB 开发人员,但到目前为止我还没有找到一件事.在 VB 中,当我设置一个新项目时,我可以指定项目中使用的命名空间并将它们添加到所有类的默认导入中(所以一切都会自动拥有它们,就好像我为每个类添加了Imports System.Data.OracleClient"一样).使用 C#,我发现我总是为每个新类输入这些内容.有没有办法为项目设置默认值,以便它至少自动为我将它们附加到每个类文件中?

I am a recently converted VB developer to C#, but there is one thing thus far that I haven't been able to find. In VB when I setup a new project I can specify the namespaces used in the project and add them to the default imports for all classes (so everything automatically has them as if I added "Imports System.Data.OracleClient" to each class). With C# I've found that I'm always typing these in for each new class. Is there a way to setup defaults for projects so it at least appends those to every class file for me automatically?

推荐答案

在 C# 10 中,这个答案发生了变化.

With C# 10 this answer has changed.

C# 10 引入了[全局使用指令][1]:

C# 10 introduces [Global using directives][1]:

全局 using 指令您可以将全局修饰符添加到任何 using 指令,以指示编译器该指令适用于编译中的所有源文件.这通常是项目中的所有源文件.

Global using directives You can add the global modifier to any using directive to instruct the compiler that the directive applies to all source files in the compilation. This is typically all source files in a project.

这篇关于C# 是否支持像 VB.NET 一样的项目范围的默认命名空间导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆