Skip to content

VisualTypeConverter loops over all assemblies looking for IVisual types #4937

@eerhardt

Description

@eerhardt

Description

We are getting trimmer warnings here:

C:\git\maui\src\Controls\src\Core\Visuals\VisualTypeConverter.cs(75,26): Trim analysis warning IL2026: Microsoft.Maui.Controls.VisualTypeConverter.Register(Assembly,Dictionary<String,IVisual>): Using member 'System.Reflection.Assembly.GetExportedTypes()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types might be removed. [C:\DotNetTest\MauiTest\MauiTest.csproj]
C:\git\maui\src\Controls\src\Core\Visuals\VisualTypeConverter.cs(77,7): Trim analysis warning IL2062: Microsoft.Maui.Controls.VisualTypeConverter.Register(Assembly,Dictionary<String,IVisual>): Value passed to parameter 'visual' of method 'Microsoft.Maui.Controls.VisualTypeConverter.Register(Type,Dictionary<String,IVisual>)' can not be statically determined and may not meet 'DynamicallyAccessedMembersAttribute' requirements. [C:\DotNetTest\MauiTest\MauiTest.csproj]

The issue is that the VisualTypeConverter looks at all the assemblies in the process looking for types that implement IVisual:

void InitMappings()
{
var mappings = new Dictionary<string, IVisual>(StringComparer.OrdinalIgnoreCase);
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
// Check for IVisual Types
foreach (var assembly in assemblies)
Register(assembly, mappings);
if (Internals.Registrar.ExtraAssemblies != null)
foreach (var assembly in Internals.Registrar.ExtraAssemblies)
Register(assembly, mappings);

static void Register(Assembly assembly, Dictionary<string, IVisual> mappings)
{
if (assembly.IsDynamic)
return;
try
{
foreach (var type in assembly.GetExportedTypes())
if (typeof(IVisual).IsAssignableFrom(type) && type != typeof(IVisual))
Register(type, mappings);

This is not trim-safe, since Types that are not referenced by IL can be trimmed by the trimmer. But in the converter code, it takes a string object and then tries looking up in the "registered mapping" for the IVisual by the string name. This can break trimmed apps because the Type being referenced by that string may have been trimmed.

In talking with @Redth and @PureWeen this approach is out-of-date, and we should remove the assembly scanning. Instead, we should add a "trim-compatible" method for doing this registration. For example:

  1. Using an assembly-level attribute (which we already have: lower in that InitMapping method).
  2. Adding a new builder.RegisterVisual<TVisual>() method that people can add to their CreateMauiApp() method. This will register the Visual type statically, and the Types that aren't referenced can be safely trimmed.

Steps to Reproduce

  1. dotnet new maui
  2. dotnet restore
  3. dotnet build -f net6.0-android -r android-arm64 -t:Run -c Release --no-restore -p:SuppressTrimAnalysisWarnings=false -p:TrimmerSingleWarn=false /bl
  4. Inspect the trimmer warnings for mentions of VisualTypeConverter.

Version with bug

Unknown/Other (please specify)

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android

Affected platform versions

All

Did you find any workaround?

No response

Relevant log output

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions