Search code examples
c#visual-studiomethodsrefactoring

Is it possible to move a static method into another class and refactor simultaneously on Visual Studio? C#


Pardon if this has been answered somewhere before.

I am not entirely sure how to phrase this question, so I'll do an example:

Lets say we have 2 different classes, A and B. If I have a static method in class A that I normally call by using "A.Method()" in my program, would it be possible to move this method from A to B so that it automatically refactors this new location "B.Method()" everywhere in the program?

In short, I would like to refactor the "location", rather than the method's "name", as I am moving methods from many different classes into one static method library.

Thank you for any pointers you can provide.


Solution

  • Not directly, but if you're sneaky, you can do it this way.

    1. Change the name of B to C. Do not auto-refactor. This will break compilation temporarily.
    2. Change the name of A to B. This time, do allow the auto-refactor. This will change all instances of A.Method() to calls to B.Method().
    3. Move (copy and paste) the code from C back into B. This will fix the compilation errors introduced in step 1.

    On the other hand, it might be simpler just to use a traditional search and replace (ctrl+F or shift+ctrl+F).