Search code examples
c#refactoringresharper

ReSharper Refactor > Move doesn't work


I have a function I want to move to a different object. In the code, I select the function that I want to move. I use ReSharper > Refactor > Move but nothing happens.


Solution

  • Option 1: Cut and paste

    This is a new option I added to the answer. This is by far the easiest.

    1. Cut the code you want to move.
    2. Paste it in the new location.
    3. An icon will surface that you can click and choose Apply Move Refactoring.

    I'm not sure if this option will always work.

    Option 2: Add the object you want to move into as a member

    I found that Refactor > Move only works if you have that object as a member. The member must be a concrete type, not an interface. For example,

    public class MyController : Controller
    {
        // ReSharper 8.2 will give the option to move to this object only.
        private MyRepository _repo;
    
        // ...
    
        public FunctionToMove()
        {
            // Do stuff.
        }
    }
    

    It makes sense when you think about it because ReSharper wants to refactor to working code. You must have a reference to the object in order to call the "moved" method. Even so, Resharper might consider a different UI decision in this case. (Like a message)

    Option 3: Change method signature

    I was having trouble moving a private method to a static class. I changed the method from private to public static and then I could select the static class I wanted to move it to.