views:

108

answers:

1

I have a class in a namespace that I've used extensively throughout my solution. As the number of classes in this namespace has grown I realize that it would've been a better design decision to place this class (and a few others) into a sub-namespace. Is there any easy way to do this in Visual Studio(C#)?

I'm thinking something like the Class->Refactor->Rename feature.

Perhaps an extension?

+8  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
Darin Dimitrov
How would that help when you want to move an identifier into a namespace?
sbi
@sbi by renaming the namespace that the class is in into the new name?
Igor Zevaka
@Igor: Read the question again. oleks wants to partition the namespace into sub-namespaces. That means moving `NS::C` to `NS::NS1::C`. Can renaming namespaces do that?
sbi
@sbi - yes it can. You can create the new namespace (i.e. create folder structure corresponding to the new namespace) in VS and then drag and drop the types into the new folder. Resharper will now warn you that these types are in a namespace (the old one) that doesn't correspond to their folder structure (ie the new namespace). It then offers you the chance to correct them; this will updated just the types that have moved to the new or sub namespace.
Rob Levine
@Rob: Ah, thanks, I didn't know that!
sbi
I've tried resharper, and yes as Rob Levine pointed out - it will complain if your namespaces don't fit the folder structure and give you the choice of fixing that. While this works perfectly for e.g. a dll project, it's not so with a web site, where your code (by convention) should be in an App_Code directory. It suggested that I make a namespace App_Code.SomeNameSpace.SomSubNameSpace -- not very good.
oleks