Skip to content

Consider OnStartup \ OnExit methods as valid initialization \ disposal method #578

Open
@Pretasoc

Description

@Pretasoc

In Unit Tests the analyzer treats the setup and teardown methods specially in the sense, that a disposable created in a setup method and disposed in a tear down method do not generate IDISP003 and IDISP006 diagnostics.

In a wpf application, there is a simmilar method pair OnStartup and OnExit. I think it should be valid code, to initialize a disposable in OnStartUp, when its disposed in OnExit. So the following two snippets should be somewhat simmilar:

public class Foo
{
    private IDisposable _foo = null!;
    
    [SetUp]
    public void Setup()
    {
        _foo = new Disposable();
    }
    
    [TearDown]
    public void TearDown()
    {
        _foo.Dispose();
    }
}
public class FooApp : Application 
{
    private IDisposable _foo = null!;
    
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        _foo = new Disposable();
    }
    
    protected override void OnExit(ExitEventArgs e)
    {
        _foo?.Dispose();
        base.OnExit(e);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions