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

CA1822 Is Overly Aggressive #7577

Open
david-maw opened this issue Feb 15, 2025 · 0 comments
Open

CA1822 Is Overly Aggressive #7577

david-maw opened this issue Feb 15, 2025 · 0 comments

Comments

@david-maw
Copy link

Warning CA1822 Is “‘xxx’ does not access instance data and can be marked as static”, the problem is that if (as in my case) xxx happens to be a public property used by XAML marking it as static will break the connection to XAML and indeed the documentation for CA1822 points out that if xxx is public marking it static may be a breaking change.

It would be nice if I could tell it to ignore this warning for public properties, but I could not figure out how, so I set out to suppress the property in the source. The trouble is, doing that generates IDE0079 “Remove unnecessary suppression” and it’s not clear to me what’s unnecessary about this suppression.

This only becomes more challenging when you are dealing with a large body of code (I was refactoring 14,000 lines) and there are many such warnings, some actionable, some not.

There's a sample MAUI project using MVVM to illustrate the problem at https://github.com/david-maw/Warning.git, run it and tap the button to change the message.

The message comes from a string property which reads a static string, the button command invokes OnPropertyChanged to notify the page to update the displayed string.

The property definition is easy enough to change to

    public static string Msg => initialMsg ? "Hello, World!" : "Goodbye, World!";

The corresponding change to the XAML is trickier, because you must find it, but something like this works to display a message using a label control:

   Text="{x:Static vm:MainViewModel.Msg}"

Unfortunately, clicking the button will no longer change the text because OnPropertyChanged no longer notifies the page.

The only workaround I've found is to disable the warning globally, but that seems a bit drastic.

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