Patrick Szalapski
Patrick Szalapski

Reputation: 9439

Any way to refactor "repoint usages to methods on other class" after extracting a class using Resharper?

After I "Extract Class" to pull out a few methods into their own class, I am then left with the original methods wrapping calls to the new class's methods. Is there a further refactor that Resharper might enable me to do, to repoint all usages to the new methods on the new class, and then perhaps delete the old methods?

For example, after a Extract Class refactoring, I might have:

public class FooSource {
    private BarSource _barSource = new BarSource();
    public Foo GetFoo(...) {...}
    public Foo GetFooByName(...) {...}
    public Bar GetBar(...) => _barSource.GetBar(...);             // now a wrapper due to refactoring
    public Bar GetBarByName(...) => _barSource.GetBarByName(...); // now a wrapper due to refactoring
}
public class BarSource {  // my new class just generated by Resharper
    private FooSource _fooSource = FooSource();  // sometimes this reference is needed; sometimes not; not relevant to this question
    public Bar GetBar(...) {...}
    public Bar GetBarByName(...) {...}
}

From here, I want to repoint all usages of FooSource.GetBar and FooSource.GetBarByName to instead use a new instance of my new BarSource class. Is there anything that will help me do most (or even just some) of this work automatically?

Upvotes: 1

Views: 66

Answers (1)

Richard Deeming
Richard Deeming

Reputation: 31198

The "Inline Method" refactoring (Ctrl+R,I) should do the trick.

Run it against the wrapper method, and it should replace all usages with the method body.

(You can invoke it either at the method declaration, or on a call to the method.)

ReSharper - Inline Method

Upvotes: 1

Related Questions