I have a long method, and for reading clarity would like to put some of the code in a separate method开发者_如何转开发. However, that can’t be done because that code uses the variables in the method. So I would like to put that code somewhere else and tell the compiler to insert that code in “this” place when compiling. Is there any way to do that? I’m using visual studio.
Sounds like you're describing the Extract method and you can do this very easily, simply highlight the code you want to move and:
Right click -> Refactor -> Extract method -> Enter method name
Visual studio will deal with the rest for you. Read the docs here.
As others have said, the fact that you have the problem in the first place is a symptom of a larger code organization problem. Ideally your methods should be so short, and have so few variables, that you don't need to move big parts of their code somewhere else. The right thing to do is probably to extract portions of the code into their own methods, each of which performs one task and does it well.
As a stopgap measure, you could use code regions to help organize your code:
void BigMethod()
{
#region Frobbing code
FrobTheBlob();
// blah blah blah
// blah blah blah
#endregion
...
And now in Visual Studio the editor will let you collapse that region down into just a single line that says "Frobbing code".
If you have one long method that you can't split because you need to access the same locals, what you really have is another object that you haven't formally made into a class. Refactor your code, extracting the method and shared state into a class of its own, and then start refactoring the method to smaller, more manageable pieces.
class SomeClass
{
// whatever shared state of the class
// whatever methods of the class
public void MethodThatsDoingTooMuch()
{
// long method
// hard to split the method because of locals
}
}
to
class SomeClass
{
// whatever shared state of the class
// whatever methods of the class
public void MethodThatIsntDoingTooMuch()
{
bigSomethingDoer.Do();
}
}
class BigSomethingDoer
{
// shared locals are fields instead
public void Do()
{
// refactor long method into smaller methods
// shared locals are promoted to class fields
// this smaller class only does this one thing
// --> its state does not pollute another class
}
}
well what you ask could be done with macros probably, but if the code is much and not readable you should consider to refactor it and create another method which accepts those variables you have in the main method as parameters.
some Refactoring tools out there have features like extract-method where you select some code and this is moved to another method for you. I guess both ReSharper and DevExpress CodeRush have this feature but I am not 100% sure and I don't have any of them installed to try this right now.
You can use anonymous methods/lambdas to create functions that can access the local variables of the containing method.
But such long methods usually aren't necessary. Try decoupling different parts of the method so they don't need to share common local variables.
精彩评论