NativeAOT support: DynamicallyAccessedMembers annotations + TUnit test migration#4
Draft
NativeAOT support: DynamicallyAccessedMembers annotations + TUnit test migration#4
Conversation
Co-authored-by: kengwang <30862240+kengwang@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add NativeAOT support for all projects and tests
Add NativeAOT support and migrate tests to TUnit
Feb 26, 2026
…nnotations Co-authored-by: kengwang <30862240+kengwang@users.noreply.github.com>
Copilot
AI
changed the title
Add NativeAOT support and migrate tests to TUnit
NativeAOT support: DynamicallyAccessedMembers annotations + TUnit test migration
Feb 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds NativeAOT compatibility across all library projects and migrates the test project to TUnit (which supports NativeAOT via compile-time test discovery).
Core AOT fix:
Activator.CreateInstance→Array.CreateInstanceThe
IEnumerable<T>resolution path usedActivator.CreateInstance(typeof(List<>).MakeGenericType(T))— a runtime generic instantiation that NativeAOT cannot compile. Replaced withArray.CreateInstance(T, count), which returns a covariantT[]satisfyingIEnumerable<T>without JIT.Precise AOT annotations
Rather than blanket
[RequiresDynamicCode]+[RequiresUnreferencedCode]on all resolve methods, annotations are now scoped to what each method actually does:[DynamicallyAccessedMembers(PublicConstructors)]— onDependencyRelation.ImplementTypeand allType implementTypeparameters down the private resolve chain (ResolveTypeToObject→ResolveSingleton/Transient/Scoped→ResolveDescriptionWithImplementType→ResolveParameterInfos). Tells the trimmer exactly which members to preserve for registered implementation types.[RequiresDynamicCode]— retained only whereMakeGenericType/MakeGenericMethodis genuinely called:ResolveDependency/ResolveDependencies(open-generic andTask<>paths)ResolveGenericDependency/ResolveGenericDependenciesPostTypeChangeNotification/NotifyDependencyChange(notification dispatch constructsIEnumerable<T>andINotifyDependencyChanged<T>at runtime)AddRelation,DeleteRelation,DisableRelation,EnableRelation,ChangeFocusingRelation) and their extension method wrappers — all propagate through toNotifyDependencyChange[RequiresUnreferencedCode]removed fromResolveDependencies,ResolveDependency,ResolveParameterInfos— DAM now handles member preservation.[UnconditionalSuppressMessage]with justification onSafeToStringandGetParameterServiceKey, where reflection on a runtimeGetType()result is intentional and can't be statically annotated.Annotation consistency across the full chain
All annotations are propagated to interfaces (
IDepositoryResolve,IRelationDepository,INotificationHub) and all call sites (NotificationHub,DepositoryServiceProvider,DepositoryServiceProviderFactory, extension methods).AOT polyfills
DynamicallyAccessedMembersAttribute,DynamicallyAccessedMemberTypes,UnconditionalSuppressMessageAttribute, andRequiresDynamicCodeAttributepolyfills added fornetstandard2.0targets in bothDepository.AbstractionandDepository.Core.Depository.Extensionsgets a minimalRequiresDynamicCode-only polyfill;Depository.Extensions.DependencyInjectioninherits Core's polyfills via the existingInternalsVisibleTo.Library project AOT markers
All four library projects (
Depository.Abstraction,Depository.Core,Depository.Extensions,Depository.Extensions.DependencyInjection) declare:Test project migration to TUnit
Depository.Testsmigrated from xUnit to TUnit 1.17.25, which uses source generators for test discovery (NativeAOT-compatible). Key changes:[Fact]→[Test],async void→async Task,Assert.IsAssignableFrom<T>replaced with direct casts,<OutputType>Exe</OutputType>+<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>added. CI workflow updated to usedotnet run(TUnit's runner) instead ofdotnet test.Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.