有没有一种方法可以在整个解决方案中全局引用命名空间?
因此,而不是在每个代码文件中都有这些行:
因此,而不是在每个代码文件中都有这些行:
using System;
using MyNamespace;
只需声明一次,每个代码文件都可以使用它们。
顺便说一下,我正在使用Visual Studio。
using System;
using MyNamespace;
只需声明一次,每个代码文件都可以使用它们。
顺便说一下,我正在使用Visual Studio。
using Foo;
namespace X
{
using Bar;
// Foo and Bar are searched for code in here, but not Baz
}
namespace Y
{
using Baz;
// Foo and Baz are searched for code in here, but not Bar
}
global using ns
的概念,至少在某种程度上满足了原帖所寻求的要求。 - Peter LillevoldGlobalNamespace.cs
。global using System;
global using System.Linq;
global using System.Text.RegularExpressions;
global using System.Threading.Tasks;
.cs
文件中再次声明using namespace
。正如其他人所提到的,Visual Studio模板是最好的选择。
请注意,仅仅在模板中添加一个using语句并不能确保编译器能够解析您的类型。因此,如果您在每个类中都添加了一个MyNamespace的using语句,您可能还需要为您的项目添加一个程序集引用。有关更多信息,请参见C# FAQ。
global using
:https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive#global-modifier - Tohid