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, and then manually adding using Foo statements as needed to resolve compilation errors. Anyone know of a smarter way of doing this? (We're a CodeRush and Refactor! shop, but I'd be interested to hear if Resharper has support for this)
Place your cursor in the namespace name. Press Ctrl+. to trigger the Quick Actions and Refactorings menu.
You could put the caret on a namespace declaration in your C# code and press F2 on the keyboard. This should let you change the name of the namespace across the entire project, including the XAML files.
Select Edit > Refactor > Rename. Right-click the code and select Rename.
Set your cursor into the namespace and then hit F2 to rename (if F2 didn't work, try CTRL R + CTRL R or rightclick - rename). All related namespaces should be changed.
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.
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
As mentioned in the comments, this answer is now outdated. Please see the up-to-date answer below
Resharper is the only tool I am aware of what has this ability. There is also a lot of other functionality that it has that is missing in CodeRush and Refactor!
This answer applies to at least Visual Studio 2013 and 2015 with no resharper required
MyCorp.AppStuff.Api
MyCorp.AppStuff.Api.Extensions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With