Extension members trigger bogus CA1515 errors
If you have the "CA1515: Consider making public types internal" code quality rule enabled in your .NET 10 project, it will generate a bogus warning (or error, depending on your configuration) when your project includes at least one extension member.
For example, the following code
internal static class Extensions
{
extension(string s)
{
public string Hello()
{
return $"Hello, {s}!";
}
}
}
could cause the following build error (notice that it doesn't even mention the exact location of offending code):
CSC : error CA1515: Because an application's API isn't typically referenced from outside the assembly, types can be made internal
Usually, you can selectively disable a rule
- with a
#pragmadirective above the offending code#pragma warning disable CA1515 - or an
.editorconfigfile in the folder with the offending code.[*.cs] dotnet_diagnostic.CS1515.severity = none
Neither approach will work. Your only option is to convert the extension member to the old extension method syntax (as long as your extension member is a method, of course):
internal static class Extensions
{
public static string Hello(this string s)
{
return $"Hello, {s}!";
}
}
Or you can upgrade your project to .NET 11 which contains a fix for the bug in the analyzer. Too bad, it took almost a year and a new version of .NET for the fix to be released.
You can find a sample project showcasing the issue in my GitHub repository. Individual commits contain:
- extension method using old syntax not triggering the error in a .NET 10 project
- extension member using new syntax triggering the error in a .NET 10 project
- extension member using new syntax not triggering the error in a .NET 11 project
