I\'m doing some architectural cleanup that involves moving a bunch of classes into different projects and/or namespaces. Currently I\'m moving the files by hand, building, a
Visual Studio 2010 has the possibility to rename a namespace. Place the cursor over the namespace name and press F2. Or simply rename it in the code and press Shift+Alt+F10, Enter after seeing the red squiggle appear.
Reharper can also rename namespaces. Quote:
The Rename Namespace refactoring allows users to rename a specific namespace and automatically correct all references to the namespace in the code. The following usages are renamed:
- Namespace statements
- Using directives
- Qualified names of types
It's not the best outcome but can be done without plugins or tools, only with Visual Studio. Find and replace in Entire Solution, Match case, Match whole word. Find what: class name, Replace with: New.Namespace.ClassName (fully qualified class name).
If you have 100+ references of the moved class and other classes in old namespace what are not moved this is the only foolproof and free solution I found. The only case when it leads to errors is when you have same class name in other namespace.
Visual Studio 2019 provides at least 2 built-in options:
'Move to namespace...' refactoring can be triggered on any class, and VS will prompt for the target namespace.
'Change namespace to...' refactoring is provided for when the current file namespace doesn't match with the folder structure.
This can be used to move individual classes to a different namespace by:
These operation ensures that all references are updated accordingly.
There are partial solutions for VS 2015 & VS 2017 without Resharper using free extensions.
One extension which I like today (end of 2017) is the Fix Namespace VS Extension: https://marketplace.visualstudio.com/items?itemName=vs-publisher-599079.FixNamespace#overview
It analyses the folder structure of your solution and offers namespace refactoring using that. Unfortunately it isn't perfect: It doesn't track dependencies that well, but solved the lion's share of the work for me.
If you cannot, or do not want to use Re$harper, Notepad++ is your friend:
DONE
This answer applies to at least Visual Studio 2013 and 2015 with no resharper required
MyCorp.AppStuff.Api
MyCorp.AppStuff.Api.Extensions