Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create an attribute that tells rule CA2000 this method calls internally Dispose() #7550

Open
HugoRoss opened this issue Jan 27, 2025 · 0 comments

Comments

@HugoRoss
Copy link

Analyzer

Diagnostic ID: CA2000

Describe the improvement

I would like to have a new attribute (e.g. [AlternativeDisposeMethod]) that tells the analyzer that a certain method calls dispose internally and that can be applied to an instance method or an extension method and then (obviously) have the analyzer adjusted to respect it.

e.g. a Stream class could declare a Close() method that has this attribute:

public class Stream : IDisposable {

    [AlternativeDisposeMethod]
    public void Close() {
        Dispose();
    }

}

or a static type that has a dispose helper method:

public static class DisposableExtensions {

    [AlternativeDisposeMethod]
    public static void TryDispose(this IDisposable? disposable) {
        if (disposable is null) return;
        try {
            disposable.Dispose();
        } catch {
        }
    }

}

Describe suggestions on how to achieve the rule

The CA2000 analyzer rule is additionally checking for existence of this attribute and if it is found, treat it like the Dispose method was called.

Additional context

It's not only an analyzer extension but would also need a .NET extension.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant