开发者

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

开发者 https://www.devze.com 2022-12-29 03:13 出处:网络
What is the easiest way of pulling an existing method out of its class and into a n开发者_JAVA技巧ew class using Visual studio 2010 / Resharper?

What is the easiest way of pulling an existing method out of its class and into a n开发者_JAVA技巧ew class using Visual studio 2010 / Resharper?

Edit: I use Resharper version 5.


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


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"


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消