How to enforce C # root namespace in all project files if they are not encoded?

I want to force the root namespace to the contents of any .cs source files that do not have their contents enclosed in an explicit namespace . In other words, I want to save classes and other space level structures from the default namespace.
(Work inside Windows.NET with Visual Studio)

In the following example, I want to force the Car class into the MotorIndustry namespace by MotorIndustry , although it does not have an explicit namespace.

Vehicle.cs (has a namespace)

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

Car.cs (no namespace / default)

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

Is there a way to accomplish this โ€œroot namespaceโ€ behavior using the project settings in Visual Studio, the AssemblyInfo.cs file, or some other means?

My understanding of VB.NET has such a function, but does C # act differently?

In some context, why I ask about this: I have hundreds of classes, and the programmer forgot to wrap the namespace around some. When I refer to an assembly from other projects, it pollutes the default namespace with classes, which ultimately cause some ambiguous situations.

+4
source share
2 answers

Use ReSharper to move classes to the appropriate namespace. In fact, version 5.0 (still in beta) allows you to fix namespaces globally.


Another way to do this is to force another developer to fix the code.

+5
source

It looks like you are trying to replicate the VB.Net project namespace function in C #. This is not a supported C # compiler or IDE function. You cannot create it by changing the project file. You will need to add a namespace for each file in the project, either manually or using a tool.

+5
source

Source: https://habr.com/ru/post/1301232/