How to pull a method out of its class (and into a new or existing)?

与世无争的帅哥 提交于 2019-12-10 03:14:28

问题


What is the easiest way of pulling an excisting method out of its class and into a new class using Visual studio 2010 / Resharper?

Edit: I use Resharper version 5.


回答1:


Starting with

public void Method() {}

  1. First, make the method static using the "Make Method Static" command.

    public static void Method(){}

  2. Then, add a local variable of the type of the new class:

    public static void Method(){Class2 me = new Class2();}

  3. Then, use Introduce Parameter

    public static void Method(Class2 me) {}

  4. Then use "Make Method non-Static". In class2:

    public void Method(){}




回答2:


Same as above, but I would not do the conversion to static-method manually. Pull up the "Refactor this" menu (using shortcuts of course, ctrl+shift+R), then select "Make method static", then "Refactor this"->"Move".

Note:

If you're talking about moving a method in a class hierarchy, you can use "Push members down" or "Pull members up"




回答3:


In up-to-date Resharper, there's Move Refactoring. You either press F6 while the cursor is in method signature, or cut-paste code to the new location, and Resharper offers you to apply the refactoring.



来源:https://stackoverflow.com/questions/2756280/how-to-pull-a-method-out-of-its-class-and-into-a-new-or-existing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!