Description
The TextColorToGenerator source generator does not account for type accessibility when scanning the Microsoft.Maui.Controls assembly. This causes it to generate extension methods for internal (non-public) types, which results in CS0122 ("type is inaccessible due to its protection level") compile errors in consuming projects.
Root Cause
In TextColorToGenerator.cs, the GetMauiInterfaceImplementors method scans all namespace-level types in the Maui Controls assembly that implement ITextStyle and IAnimatable:
static IEnumerable<INamedTypeSymbol> GetMauiInterfaceImplementors(...)
{
return mauiControlsAssemblySymbolProvider.GlobalNamespace.GetNamedTypeSymbols()
.Where(x => x.AllInterfaces.Contains(itextStyleSymbol, SymbolEqualityComparer.Default)
&& x.AllInterfaces.Contains(iAnimatableSymbol, SymbolEqualityComparer.Default));
}
This does not filter by DeclaredAccessibility, so it picks up internal types too.
The GenerateMetadata method does set the generated extension class access modifier to internal for Maui types, but the generated code still references the original type by its fully qualified name (e.g., global::Microsoft.Maui.Controls.ContentLabel) in the method signature and body. Since that type is internal to the Maui Controls assembly, the generated code in the consuming project cannot compile.
Additionally, the GetClassAccessModifier helper only handles Public and Internal — it does not handle other accessibility levels and does not contribute to filtering out types that should not have code generated for them.
Steps to Reproduce
- Have a type with
internal accessibility in the Microsoft.Maui.Controls namespace that implements both ITextStyle and IAnimatable (e.g., internal class ContentLabel : Label)
- Use CommunityToolkit.Maui in a project
- Build → CS0122 errors referencing the internal type in the generated
TextColorTo extension
This was triggered by dotnet/maui#31940 which added an internal class ContentLabel : Label to Microsoft.Maui.Controls. See dotnet/maui#34512 for the original user report.
Suggested Fix
The GetMauiInterfaceImplementors method should filter out types that are not publicly accessible:
static IEnumerable<INamedTypeSymbol> GetMauiInterfaceImplementors(...)
{
return mauiControlsAssemblySymbolProvider.GlobalNamespace.GetNamedTypeSymbols()
.Where(x => x.DeclaredAccessibility == Accessibility.Public
&& x.AllInterfaces.Contains(itextStyleSymbol, SymbolEqualityComparer.Default)
&& x.AllInterfaces.Contains(iAnimatableSymbol, SymbolEqualityComparer.Default));
}
Workaround
A workaround was merged on the .NET MAUI side in dotnet/maui#34514, which moved ContentLabel from a top-level internal class to a nested (private) class so it is no longer visible to namespace-level scans. However, this is a workaround — the source generator should be resilient to non-public types in referenced assemblies.
Description
The
TextColorToGeneratorsource generator does not account for type accessibility when scanning theMicrosoft.Maui.Controlsassembly. This causes it to generate extension methods forinternal(non-public) types, which results in CS0122 ("type is inaccessible due to its protection level") compile errors in consuming projects.Root Cause
In
TextColorToGenerator.cs, theGetMauiInterfaceImplementorsmethod scans all namespace-level types in the Maui Controls assembly that implementITextStyleandIAnimatable:This does not filter by
DeclaredAccessibility, so it picks upinternaltypes too.The
GenerateMetadatamethod does set the generated extension class access modifier tointernalfor Maui types, but the generated code still references the original type by its fully qualified name (e.g.,global::Microsoft.Maui.Controls.ContentLabel) in the method signature and body. Since that type isinternalto the Maui Controls assembly, the generated code in the consuming project cannot compile.Additionally, the
GetClassAccessModifierhelper only handlesPublicandInternal— it does not handle other accessibility levels and does not contribute to filtering out types that should not have code generated for them.Steps to Reproduce
internalaccessibility in theMicrosoft.Maui.Controlsnamespace that implements bothITextStyleandIAnimatable(e.g.,internal class ContentLabel : Label)TextColorToextensionThis was triggered by dotnet/maui#31940 which added an
internal class ContentLabel : LabeltoMicrosoft.Maui.Controls. See dotnet/maui#34512 for the original user report.Suggested Fix
The
GetMauiInterfaceImplementorsmethod should filter out types that are not publicly accessible:Workaround
A workaround was merged on the .NET MAUI side in dotnet/maui#34514, which moved
ContentLabelfrom a top-levelinternalclass to a nested (private) class so it is no longer visible to namespace-level scans. However, this is a workaround — the source generator should be resilient to non-public types in referenced assemblies.