Fix IL2091 and CsWinRT1028 warnings for trimming and Native AOT support#7
Fix IL2091 and CsWinRT1028 warnings for trimming and Native AOT support#7
Conversation
Co-authored-by: nlogozzo <17648453+nlogozzo@users.noreply.github.com>
Co-authored-by: nlogozzo <17648453+nlogozzo@users.noreply.github.com>
|
@copilot Does this limit the use of this library in NativeAOT, or no it will still work? How would someone call the ConfigureAdw and ConfigureWinUI in a native aot supported client app? |
It does not limit usage in Native AOT — the methods will still compile and run correctly in AOT apps.
A Native AOT client app would call it exactly the same way as any other app. The only difference is handling the IL3050 warning at the call site. They have two options: Option 1 — propagate the constraint up (recommended if the startup method itself is not AOT-safe): [RequiresDynamicCode("Uses ConfigureAdw which requires dynamic code.")]
static void Main(string[] args)
{
var builder = Host.CreateApplicationBuilder(args);
builder.ConfigureAdw<MainWindow>();
builder.Build().Run();
}Option 2 — suppress the warning (acceptable when the developer knows T's constructors are fully preserved): #pragma warning disable IL3050
builder.ConfigureAdw<MainWindow>();
#pragma warning restore IL3050In practice, most Native AOT app entry points will either already be annotated or the developer will suppress it intentionally, so it's a clean and standard pattern. |
|
@copilot Can we remove the |
Unfortunately no — the two attributes solve different problems and both are needed here.
The DI container ( So in short: |
|
@copilot Any other way to solve this AddSingleton issue without all these annotations? |
There is one alternative that avoids both annotations: accept a factory delegate instead of using the reflective The factory-based overload public IHostApplicationBuilder ConfigureAdw<T>(Func<IServiceProvider, T> windowFactory, ...) where T : Adw.ApplicationWindow
{
// ...
builder.Services.AddSingleton<T>(windowFactory);
// ...
}and callers would write: builder.ConfigureAdw<MainWindow>(sp => new MainWindow(...));Trade-offs vs. the current approach:
The current annotation approach keeps the simpler |
Build emitted IL2091 (
DynamicallyAccessedMembersconstraint not propagated) and CsWinRT1028 (WinRT interface implementors notpartial) warnings, blocking clean trimmed/AOT builds.Changes
[DynamicallyAccessedMembers]on genericT— annotated the type parameter inConfigureAdw<T>andConfigureWinUI<T>to satisfy theAddSingleton<T>trimmer requirement:[RequiresDynamicCode]on both extension methods — propagates the Native AOT constraint to callers since reflection-based DI may emit code at runtime.partialon all 7 WinUI converters —BoolToDoubleConverter,BoolToStyleConverter,BoolToVisibilityConverter,IntToBoolConverter,NullableToVisibilityConverter,StringToVisibilityConverter,VisibilityToBoolConverter— required for the CsWinRT source generator to produce AOT-compatible WinRT interop.📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.