Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting extension methods to instance methods with ReSharper?

Tags:

Suppose I have a class:

public class Foo
{
    public int Bar { get; set; }
}

And an extension method:

public static class FooExtensions
{
    public static void DoSomething(this Foo foo, int someNumber)
    {
        foo.Bar += someNumber;
    }
}

What would be the most convenient way to remove the FooExtensions class and alter Foo so it becomes:

public class Foo
{
    public int Bar { get; set; }

    public void DoSomething(int someNumber)
    {
        Bar += someNumber;
    }
}

ReSharper doesn't seem to offer a one-step action to do this (strangely enough as this seems a pretty straightforward refactoring), but which other actions might I combine to avoid too much copy/pasting around and manual work?

like image 533
MarioDS Avatar asked Jun 06 '16 15:06

MarioDS


1 Answers

Apparently, there is a ReSharper action for this, but it is named a bit awkwardly (imho). It's called "Make Method Non-Static".

The documentation can be found here. Note that it doesn't mention extension methods specifically.

The same refactoring will work for other kinds of static methods (non-extension), which probably explains the naming.

like image 157
MarioDS Avatar answered Sep 28 '22 03:09

MarioDS