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

False positive of CA1515 on "partial class" code for Blazor Components #7473

Open
Daeymon opened this issue Nov 15, 2024 · 2 comments
Open

Comments

@Daeymon
Copy link

Daeymon commented Nov 15, 2024

CA1515: Consider making public types internal

Blazor components using a "partial class" for its code rather than a @code block on the razor page itself is getting this warning on every "partial class". Blazor components are restricted to being public, which means this warning cannot be resolved.

Can be easily replicated with the .NET 8 Blazor Web App template.
Add the following to the project file:

	<EnableNETAnalyzers>true</EnableNETAnalyzers>
	<AnalysisMode>AllEnabledByDefault</AnalysisMode>
	<AnalysisLevel>latest</AnalysisLevel>

Then create a file named "Counter.razor.cs" in the pages folder and add the following code to it:

namespace BlazorApp1.Components.Pages
{
    public partial class Counter
    {
        private int currentCount = 0;

        private void IncrementCount()
        {
            currentCount++;
        }
    }
}

Then remove the @code block from the "Counter.razor" file.

Warning CA1515 will now appear on "Counter.razor.cs", which is a false positive. That partial class cannot be internal due to Blazor restrictions.

@Daeymon
Copy link
Author

Daeymon commented Nov 15, 2024

This issue also extends to ApiControllers, which also have to be public.
Image

And types utilised by Blazor component parameters, which again, have to be public.
Image
Image

@Thieum
Copy link

Thieum commented Nov 15, 2024

It doesn't work for the same reasons with wpf windows partial classes.

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

2 participants