【问题标题】:Any way to refactor "repoint usages to methods on other class" after extracting a class using Resharper?使用 Resharper 提取类后,有什么方法可以重构“将用法重新指向其他类的方法”?
【发布时间】:2017-10-17 20:46:29
【问题描述】:

在我“提取类”以将一些方法提取到他们自己的类中之后,我就剩下原始方法包装对新类方法的调用。 Resharper 是否可以让我做进一步的重构,将所有用法重新指向新类的新方法,然后可能删除旧方法?

例如,在一个 Extract Class 重构之后,我可能有:

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(...) {...}
}

从这里开始,我想重新指出 FooSource.GetBar 和 FooSource.GetBarByName 的所有用法,以改用我的新 BarSource 类的新实例。有什么可以帮助我自动完成大部分(甚至只是部分)这项工作吗?

【问题讨论】:

    标签: c# resharper


    【解决方案1】:

    "Inline Method" refactoring (Ctrl+R,I) 应该可以解决问题。

    针对包装方法运行它,它应该用方法体替换所有用法。

    (您可以在方法声明或方法调用时调用它。)

    【讨论】:

    • 谢谢——我不认为这个操作是“内联”的,但它似乎很合适。我会试一试。是否可以同时在多个方法上这样做?
    • @PatrickSzalapski 我不这么认为;您必须单独内联每个方法。
    猜你喜欢
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 2019-01-08
    • 2023-02-14
    • 2021-07-16
    相关资源
    最近更新 更多