Refactoring namespaces in vs2005

Using the refactor option within vs2005 is great when renaming a type definition.
However - I have the need to move several types from one namespace to another.  Is there a way of doing this similar to the refactor, that will inspect my project - and update any references as well

Specifically what I am doing is the following:

I have several type defined in Namespace A...
A.T1
A.T2
A.Tn
Elsewhere in my code I refer to these Types either by:
using A;
...
T1
or explicitly:
A.T1


I have the requirement to rename the namespace A, to B.C thus defining the following:
B.C.T1
B.C.T2
B.C.Tn

Is there a function within VS2005 that allows me to refactor the namespace A to B.C - hence update my references to those defined types

Cheers,
G.



Answer this question

Refactoring namespaces in vs2005

  • Jeet

    Gerard,

    The refactoring that you're referring to (move type) is one that we've considered doing, and will likely do in the future.  It ends up being a bit different from a rename, because it must make sure to gaurantee that all symbols which were bound previously continue to be bound.  For example:

    namespace Source
    {
      class Base {}
    }


    namespace Source
    {
      class Derived: Base {}
    }

    namespace Destination
    {
    }

    Let's suppose that I want to move Derived from Source to Destination.  In this case, Base (in the bases and interface list of Derived) would need to be modified to be a fully qualified type name, or the namespace would need to have a using directive to bring Source.Base into scope. 

    We would most likely expose this in classview, through some UI in the editor, and through class designer (and potentially other places). 

    Good suggestion!
    Anson Horton [ansonh@microsoft.com]
    Visual C# IDE PM

  • Ronald Widha

    Thanks Anson,
    Would be good to have this feature eventually.
    I understand that there are token resolution difficulties in extending the refactoring to movement of types between namespaces.
    However - it's also something that happens quite frequently, especially in prototyping, as you determine more logical layouts and names for your model hierarchy as the product progresses.
    - Nice to know that it's on the wish-list for the future though.
    Cheers,
    G.

  • diego Castelli

    Solved - as per normal, quite simple..

    Simply right clicking on the namespace portion you want to refactor - and selecting "refactor"..

    However - there are a number of scenarios that this doesn't support....

    E.g.

    namespace A.B.C
    Changing A, B or C to D works
    changing A to A.D, B to B.D & C to C.D works...

    But going back in the opposite direction doesn't work.

    If I wanted to change A.B to simply D, I get the following error:
    ---------------------------
    Microsoft Visual Studio
    ---------------------------
    Please select a symbol to be renamed.
    ---------------------------
    OK  
    ---------------------------
    I.e. - it doesn't like a range selection, only a cursor inside a specific token.

    So - it seems as though there is no way for me flatten a namespace structure through refactor in the beta.

    What would be better - is enabling you to drag type definitions from one namespace to another through the class view.  That would be handy - and make a lot more sense, as well as being able to rename a type in the class view - and perhaps a prompt to ask if you want to refactor references...


  • Refactoring namespaces in vs2005