Vannevelj / VSDiagnostics

A collection of static analyzers based on Roslyn that integrate with VS
GNU General Public License v2.0
65 stars 16 forks source link

Convert inline event handler to method #596

Closed Hosch250 closed 8 years ago

Hosch250 commented 8 years ago

This is basically going to be a sort of Extract Method. I'm not sure how we could handle the use of variables/parameters in the parent method--perhaps we should just ignore those?

Vannevelj commented 8 years ago

Can you clarify with an example?

Hosch250 commented 8 years ago

For example, suppose I have the following code:

public void Something()
{
    _somethingElse.Event += (sender, e) =>
    {
        [...]
    }
}

I'd like to be able to extract the content of that handler to a method with the signature provided just before the => operator. I don't want it to try to figure out the types--I'll just have to handle that, if necessary. Putting event handlers inline like that is a memory leak, and they can't ever be unsubscribed from, and in codebases with a lot of them, it would save arm-strain when handling the mouse (well, if your arm is already hurting).

Something like:

public void Something()
{
    _somethingElse.Event += _somethingElseEventHandler;
}
private void _somethingElseEventHandler(object sender, EventArgs e)
{
    [...]
}

It would probably be best for VSD if this was just an analyzer without a code fix--I'd just make a personal code-fix for myself.

Vannevelj commented 8 years ago

So.. #591 ?

Hosch250 commented 8 years ago

Oh, yeah.