Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to move a class from one namespace to the another, programmatically?

I want to move a class form one namespace to another, programmatically. This includes adjusting any dependencies the moved class had in its previous namespace.

I'm guessing that I can make use of the Roslyn project somehow, but I can't finding a starting point.

Edit:

I'm trying to implement an automatic move class refactoring on C# code. Doing it for study purposes - gathering code metrics and studying the changes before and after the refactoring process.

I can do the refactorings by hand, but was wondering If I can do it automatically. This means that I already have the refactoring candidates and their proposed move locations.

like image 608
Teodor Kurtev Avatar asked Jan 31 '17 09:01

Teodor Kurtev


People also ask

How to move a class from one namespace to another?

This can be used to move individual classes to a different namespace by: applying the mentioned refactoring (CTRL+. with the cursor over the namespace) These operation ensures that all references are updated accordingly. Move to namespace doesn't change references. I don't have Change namespace to, is it Enterprise only feature?

What is the use of a namespace in Java?

A namespace is designed for providing a way to keep one set of names separate from another. The class names declared in one namespace does not conflict with the same class names declared in another.

What is a namespace in C++?

A namespace is designed for providing a way to keep one set of names separate from another. The class names declared in one namespace does not conflict with the same class names declared in another. A namespace definition begins with the keyword namespace followed by the namespace name as follows −.

Do the class names declared in one namespace conflict with each other?

The class names declared in one namespace does not conflict with the same class names declared in another. A namespace definition begins with the keyword namespace followed by the namespace name as follows − To call the namespace-enabled version of either function or variable, prepend the namespace name as follows −


1 Answers

There is no built-in support for this, so yes, you'd have to implement this yourself.

If you did want to try this, there's some high level steps you'd probably do:

  1. You'd first call SymbolFinder.FindReferences to figure out where your type name is being mentioned.
  2. For each document you find a reference to, use a SyntaxRewriter to rewrite the syntax.
  3. Put the new syntax there.
  4. Run our simplifier process to get everything cleaned back up.

You could dig through our rename code to see how we do this, but I'll warn you it's fairly complex. This refactoring is probably "medium" in terms of difficulty, so not to dissuade you but you have an uphill battle if you tried this as your first introduction to Roslyn.

(Motto: refactorings are always harder than you think they are.)

like image 163
Jason Malinowski Avatar answered Oct 19 '22 10:10

Jason Malinowski