Repository navigation
Conversation
Add the API approved in dotnet#136546. It returns the interfaces implemented or inherited by a type that have the same metadata definition as the specified interface, which gives a trim-safe way to find all instantiations of a generic interface. Fixes dotnet#136546 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @steveisok, @dotnet/area-system-reflection |
|
Move matching interfaces to the front of the array returned by GetInterfaces and trim it with Array.Resize instead of iterating twice. Throw ArgumentException for constructed generic interfaces; callers pass the generic type definition instead. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks for the review!
Also, per @agocke's feedback on the issue, constructed generic interfaces now throw |
Yes |
Replace loops over GetInterfaces() that filter by a generic interface definition in System.Linq.Queryable and DataContract serialization. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Done in eedc3d6:
The order of the results matches |
|
Can use this API? |
Update GetCompatibleGenericInterface in ReflectionExtensions.cs to use Type.GetInterfacesWithSameMetadataDefinitionAs under NET12_0_OR_GREATER. Addresses review feedback from @jkotas on dotnet#137016.
|
Can these use the new API? runtime/src/libraries/System.Text.Json/src/System/ReflectionExtensions.cs Lines 173 to 205 in bbde556 |
Replace GetInterfaces() loops filtering by a generic interface definition in System.Linq.Queryable (TypeHelper), Microsoft.Extensions.Configuration.Binder, System.Diagnostics.DiagnosticSource and System.Text.Json. Multi-targeted libraries keep the existing loop for frameworks without the new API. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Yes, all four can. Updated in 83ac9e2:
Configuration.Binder, DiagnosticSource and System.Text.Json also target older frameworks, so they keep the existing loop under |
Replace the #if NET12_0_OR_GREATER fallbacks in System.Text.Json, Microsoft.Extensions.Configuration.Binder and System.Diagnostics.DiagnosticSource with a TypePolyfills extension for older target frameworks, and remove the IL2070 suppressions that were unused on net12.0 (IL2121). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…itionAs polyfill Include the polyfill only for target frameworks older than net12.0, so it stops being compiled for .NETCoreApp once NetCoreAppMinimum moves to .NET 12. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…thSameMetadataDefinitionAs Most calls match a single interface, which ListBuilder handles without allocating backing storage, and RuntimeTypes can be compared by reference. Don't presize the builder for every candidate: that over-allocated when several interfaces matched. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ameMetadataDefinitionAs The candidates are compared by reference, so an interfaceType that isn't a RuntimeType never matches. Validate interfaceType directly and add a test for a non-runtime interface type. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…DefinitionAs For types with a non-generic-definition MethodTable, walk the interface map and compare MethodTables directly, creating Type objects only for the matches. Other cases fall back to the base implementation. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Compare the generic definition of each implemented interface (or the interface itself when it isn't generic) with interfaceType, instead of branching on whether interfaceType is a generic type definition.
Match GetMemberWithSameMetadataDefinitionAs: only null throws. A constructed generic interface matches every instantiation of its definition, and a type that isn't an interface matches nothing. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Move the GetInterfacesWithSameMetadataDefinitionAs polyfill into TypePolyfills.cs, remove a DynamicallyAccessedMembers annotation and a cached flag that are no longer needed, and use the same indexing pattern in System.Text.Json as at the other call sites. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…definition Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
The tests explicitly root expected interface instantiations, leaving discovery without separate rooting and trimming-warning behavior unverified under ILLink and NativeAOT.
1 open issue
What changed in this PR
Adds Type.GetInterfacesWithSameMetadataDefinitionAs to support trim-safe interface discovery by metadata definition, addressing the scenario described in #136546.
Changes:
- Adds the public API, documentation, and specialized CoreCLR and NativeAOT implementations.
- Migrates library interface searches to the new API and provides a polyfill for older targets.
- Adds reflection tests for matching, inheritance, generic types, and argument validation.
| File | Description |
|---|---|
| src/libraries/System.Text.Json/src/System/ReflectionExtensions.cs | Uses the API for generic interface enumeration. |
| src/libraries/System.Text.Json/src/System.Text.Json.csproj | Includes the polyfill on older targets. |
| src/libraries/System.Text.Json/Common/ReflectionExtensions.cs | Simplifies compatible interface lookup. |
| src/libraries/System.Runtime/tests/System.Reflection.Tests/TypeInfoTests.cs | Adds API behavior and validation tests. |
| src/libraries/System.Runtime/ref/System.Runtime.cs | Declares the public API. |
| src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/XmlFormatWriterGenerator.cs | Filters XML collection enumerator interfaces. |
| src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonFormatWriterGenerator.cs | Filters JSON collection enumerator interfaces. |
| src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/CollectionDataContract.cs | Updates collection enumerator discovery. |
| src/libraries/System.Private.CoreLib/src/System/Type.cs | Implements and documents the default API. |
| src/libraries/System.Linq.Queryable/src/System/Linq/TypeHelper.cs | Simplifies generic interface discovery. |
| src/libraries/System.Linq.Queryable/src/System/Linq/EnumerableRewriter.cs | Uses filtered enumerable interface lookup. |
| src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DsesFilterAndTransform.cs | Updates enumerable property-fetch discovery. |
| src/libraries/System.Diagnostics.DiagnosticSource/src/System.Diagnostics.DiagnosticSource.csproj | Includes the polyfill on older targets. |
| src/libraries/Microsoft.Extensions.Configuration.Binder/src/Microsoft.Extensions.Configuration.Binder.csproj | Includes the polyfill on older targets. |
| src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs | Simplifies open generic interface lookup. |
| src/libraries/Common/src/Polyfills/TypePolyfills.cs | Adds the downlevel API equivalent. |
| src/coreclr/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs | Filters cached runtime interfaces directly. |
| src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeType.NativeAot.cs | Adds direct interface-map matching with fallback. |
Review effort: Balanced

Implements the API approved in #136546:
The implementation follows
GetMemberWithSameMetadataDefinitionAs: it filtersGetInterfaces()withHasSameMetadataDefinitionAs, and suppresses IL2085 with the same reasoning as the existingImplementInterfacehelper. IfinterfaceTypeexists, trimming kept the matching interface implementations, so the result is complete relative to what's reachable.Behavior
The approval doesn't spell these out. Following review, the behavior matches
GetMemberWithSameMetadataDefinitionAs:interfaceTypecan be any type, as withGetMemberWithSameMetadataDefinitionAs. A generic interface type definition (typeof(IList<>)) or any of its instantiations matches every instantiation the type implements, soIList<int>also matches an implementedIList<string>. A type that isn't an interface matches nothing.GetInterfaces(). An interface type doesn't match itself.nullthrowsArgumentNullException; nothing else throws.Type.EmptyTypes. The order of the results isn't defined.Testing
New tests in
TypeInfoTestscover generic definitions and constructed forms, base-class and interface inheritance, non-generic interfaces, no match, a generic type definition as the current type, framework types (int,List<string>, arrays), and argument validation. The same cases were also run against a copy of the implementation on .NET 10. CoreLib (with the trimming analyzers), the System.Runtime ref (including API compat) and System.Reflection.Tests build cleanly.Fixes #136546
🤖 Generated with Claude Code