1

I have a C# project and I am mapping a lot of classes from Json models.

Some of the names are colliding so I have to setup namespaces every time. I generate the classes with an online tool to create C# classes from JSON, so by default the classes don't have a namespace.

Is there any way, with right click on the class or some tool on Visual Studio 2019, that allows me to automaticaly select a bunch of classes and set a namespace for them? Or right click a class and set it's namespace as it's path in the filesystem from the workspace folder?

The problem is that right now, my only chocie is copying the namespace, pasting it on every class and surrounding the code with brackets again and again. I just want to make this proces easier.

Also I know it's not needed to place every single class in a namespace, but to prevent execution time errors and keep the code sorted, I prefer to do so.

  • Perhaps what you are looking for is additional projects within your solution, that automatically generates a new namespace for you, class libraries for example. – Freek W. May 31 '21 at 11:23
  • 1
    _" I generate the classes with an online tool to create C# classes from JSON, so by default the classes don't have a namespace."_ - if you create an empty C# file first, put in a namespace _then_ perform a **Edit | Paste Special | JSON as classes** not only does it mitigate the online tool but also the generated code will appear in the namespace you created earlier –  May 31 '21 at 11:27
  • 2
    Does this answer your question? [How to show the "paste Json class" in visual studio 2012 when clicking on Paste Special?](https://stackoverflow.com/questions/18526659/how-to-show-the-paste-json-class-in-visual-studio-2012-when-clicking-on-paste) –  May 31 '21 at 11:27
  • Not really, because I was looking for something that generated the classes in different files. Anyway i found another tool that alows me to set the namespaces before generating the classes. On the other hand, I inherited the project and it's quite a mess. If someone can find a tool that allows changing the namespaces without actually haviing to go in to each class to do so would be helpful. – Francesc Bosch May 31 '21 at 11:35
  • https://github.com/quicktype/quicktype-vs –  May 31 '21 at 11:51
  • @FrancescBosch _"`Not really`, because I was looking for something that generated the classes in `different files`"_ - though you mention multiple classes you didn't make it evident that you are generating _multiple files_. _"right click a class and set it's namespace as it's path in the filesystem"_ - can be interpreted both ways –  Jun 02 '21 at 06:00

3 Answers3

3

You can Paste JSON As Classes in Visual Studio.

Paste JSON As Classes

It will create the classes with the current namespace. Simply create a new class, "Paste JSON As Classes" and it will create every model from the JSON on the clipboard.

silentw
  • 4,835
  • 4
  • 25
  • 45
  • This kind of solves the problem, even though I was looking for something specifically to change the namespaces from already existing classes easly. I managed to find another online tool that allows to set the namespace beforehand so that at least the new classes I generate won't have this problem, but there's already quite a mess in the project and Iwill still need something to change already existing classes's namespaces. If someone is curious the webpage that allows to set the namespace and geenrate the classes in different files is this one: https://json2csharp.com/ – Francesc Bosch May 31 '21 at 11:40
2

You can user Resharper to adjust namespaces of existing classes. https://www.jetbrains.com/help/resharper/Fix_inconsistent_namespace_naming.html enter image description here

Abhijeet
  • 13,562
  • 26
  • 94
  • 175
1

@ Francesc Bosch.

For changing the namespace of an existing class, you can right click on your current namespace and select Rename -> change to your new namespace-> click Apply.

If you have multiple depths of the namespace, Visual Studio will not allow you to type dots. However, if you copy and paste a dot, it will succeed despite the warning.

You could also move the class to the changed target namespace.

1.Right-click the name of the class you want to move -> select Quick Actions and Refactorings... -> click Move to namespace...

enter image description here

2.In the dialog box that opens, select the target namespace you'd like to move the type to.

enter image description here

Hui Liu-MSFT
  • 770
  • 4
  • 15