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:
- Using an assembly-level attribute (which we already have: lower in that InitMapping method).
- 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
dotnet new maui
dotnet restore
dotnet build -f net6.0-android -r android-arm64 -t:Run -c Release --no-restore -p:SuppressTrimAnalysisWarnings=false -p:TrimmerSingleWarn=false /bl
- 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
Description
We are getting trimmer warnings here:
The issue is that the
VisualTypeConverterlooks at all the assemblies in the process looking for types that implementIVisual:maui/src/Controls/src/Core/Visuals/VisualTypeConverter.cs
Lines 27 to 38 in 5d8b798
maui/src/Controls/src/Core/Visuals/VisualTypeConverter.cs
Lines 68 to 77 in 5d8b798
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
stringobject and then tries looking up in the "registered mapping" for theIVisualby 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:
builder.RegisterVisual<TVisual>()method that people can add to theirCreateMauiApp()method. This will register the Visual type statically, and the Types that aren't referenced can be safely trimmed.Steps to Reproduce
dotnet new mauidotnet restoredotnet build -f net6.0-android -r android-arm64 -t:Run -c Release --no-restore -p:SuppressTrimAnalysisWarnings=false -p:TrimmerSingleWarn=false /blVisualTypeConverter.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