如何从它的类中提取一个方法(并进入一个新的或现有的)? [英] How to pull a method out of its class (and into a new or existing)?

查看:76
本文介绍了如何从它的类中提取一个方法(并进入一个新的或现有的)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Visual Studio 2010/Resharper 将现有方法从其类中提取到新类中的最简单方法是什么?

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

我使用 Resharper 版本 5.

I use Resharper version 5.

推荐答案

public void Method() {}

public void Method() {}

  1. 首先,使用使方法静态"命令将方法设为静态.

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

public static void Method(){}

public static void Method(){}

然后,添加一个新类的类型的局部变量:

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

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

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

然后,使用引入参数

public static void Method(Class2 me) {}

public static void Method(Class2 me) {}

然后使用使方法非静态".在 class2 中:

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

public void Method(){}

public void Method(){}

这篇关于如何从它的类中提取一个方法(并进入一个新的或现有的)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆