如何强制整个项目文件的C#根命名空间时,没有一个是codeD?文件、项目、空间、codeD

2023-09-06 13:10:50 作者:奇遇是恶作剧

我要强制根命名空间上的任何的.cs源文件没有包裹在一个明确的命名空间其内容的内容。换句话说,我希望保留类和其他的命名空间层次结构出了默认的命名空间。 (里面工作在Windows .NET环境与Visual Studio)

I want to force a root namespace on the contents of any .cs source files that don't have their contents wrapped in an explicit namespace. In other words I want to keep classes and other namespace-level structures out of the default namespace. (Working inside a Windows .NET environment with Visual Studio)

在下面的例子中,我想在默认情况下强制类到 MotorIndustry 命名空间,即使它没有明确的命名空间codeD。

In the following example, I want to force the Car class into the MotorIndustry namespace by default even though it has no explicit namespace coded.

Vehicle.cs (已命名)

namespace MotorIndustry {
    public class Vehicle {
        // ...
    }
}

Car.cs (没有命名空间/默认)

Car.cs (no namespace/default)

public class Car : Vehicle {
    //...
}

有没有办法来完成,通过在Visual Studio中,AssemblyInfo.cs文件或其他方式的项目设置这个根命名空间修改的行为?

我的理解是VB.NET有这样的,但C#的行为不同?

My understanding is VB.NET has a feature like this but C# acts differently?

有些情况下,为什么我会问:我有数百个类和程序员忘记环绕的一些命名空间。当我引用的程序集从其它项目中它的污染与最终导致一些模棱两可的情况下,类默认的命名空间。

Some context about why am I asking this: I have hundreds of classes and the programmer forgot to wrap the namespace around some. When I reference the assembly from other projects it's polluting the default namespace with classes that end up causing some ambiguous situations.

推荐答案

使用ReSharper的移动类到正确的命名空间。事实上,版本5.0(仍处于测试阶段),可以在全球范围内纠正的命名空间。

Use ReSharper to move the classes into the proper namespace. In fact, version 5.0 (still in Beta) allows you to correct namespaces globally.

另一种方式做到这一点是让其他开发修复code。

The other way to do it is to make the other developer fix the code.