I’d like to move a method from one class to another, which means promoting the instance variables of that method to arguments of the method in the other class.
Is there a way to do this? Something like an extract static method would be great (i.e. forcing the method to be extracted statically, which would automatically promote all the instance variables to parameters.)
For example:
string y = InstanceVariable;
return new string(y.ToCharArray().Reverse().ToArray());
Should become:
private static string ExtractedMethod(string InstanceVariable)
{
string y = InstanceVariable;
return new string(y.ToCharArray().Reverse().ToArray());
}
Thanks.
Select the line with only local variables then CTRL+R+M (or right click, refactor, extract method). If you are not using any fields in the code block being extracted it will automagically make it static.
You will need to manually ensure that there are no field references though – it doesn’t have an option to do that for you.
Plugins like ReSharper, CodeRush and JustCode should be able to juggle the method between classes; and possibly automatically remove field references.