Repository navigation
Description
Background and motivation
In Npgsql we have one quirky RUC trim suppression, a use of GetInterfaces() on a statically unknown type. As you could observe different results for the same type instance - based on which interfaces were rooted - GetInterfaces is not trim-safe today.
When we have a serialization mapping rooted for the instantiation in the final binary we care that it returns the interface we have that mapping for (in this case some constructed form of IList<T>), in any other case we don't care what does or does not come back.
There exists at least one other suppression like this in the BCL as well:
runtime/src/libraries/System.Private.CoreLib/src/System/Type.Helpers.cs
Lines 400 to 405 in 425a54e
| // IL2085 is produced due to the "this" of the method not being annotated and used in effectively this.GetInterfaces() | |
| [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2085:UnrecognizedReflectionPattern", | |
| Justification = "The GetInterfaces technically requires all interfaces to be preserved" + | |
| "But this method only compares the result against the passed in ifaceType." + | |
| "So if ifaceType exists, then trimming should have kept it implemented on any type.")] | |
| internal bool ImplementInterface(Type ifaceType) |
Both examples require completeness relative only to what remains reachable: the same retained code that makes particular interface matches relevant also ensures those matches remain discoverable.
Unsurprisingly, we'd like to replace this suppressed call with a safe API, as it's non-trivial to understand what actually makes it safe.
More relevant background from @MichalStrehovsky: #100000 (comment)
API Proposal
namespace System;
public partial abstract class Type
{
public virtual Type[] GetInterfacesWithSameMetadataDefinitionAs(Type interfaceDefinition);
}API Usage
foreach (var iface in type.GetInterfacesWithSameMetadataDefinitionAs(typeof(IList<>)))
{
...
}The trimmer would suppress the RUC warning when there is a statically provided type, which would do the rooting for the interface. As was done for other reflection apis like MakeGenericType.
The intent would be to explicitly support generic interface definitions, returning all instantiations when there is more than one implementation. This addresses the case where a closed interface type is unavailable (which could also be used in IsAssignableFrom/IsAssignableTo). I don't have an opinion on whether the api accepts closed forms if the type argument is generic as well.
Alternative Designs
No response
Risks
No response
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
dotnet-policy-service commentedon Oct 27, 2026
Tagging subscribers to this area: @agocke, @dotnet/illink
See info in area-owners.md if you want to be subscribed.
teo-tsirpanis commentedon Oct 27, 2026
Why not always be trim-safe? Because if you are passing a
Typeinstance to this method, that type must exist in the first place.And how about we call it
GetInterfacesWithSameMetadataDefinitionAs?NinoFloris commentedon Oct 27, 2026
Ah yes, I should have changed that. I was unsure what the interaction was with type definitions and ILC. Today ILC keeps all instantiations as Michal states in #100000 (comment) but I wasn't sure when I wrote it. If it wasn't that way it would be trim safe for concrete types only.
MichalStrehovsky commentedon Oct 28, 2026
Some use cases in the wild:
https://github.com/dotnet/aspnetcore/blob/f8bf4a7477a37fc09c9bdbe31b7938003b29b987/src/SignalR/common/Shared/ReflectionHelper.cs#L73-L91
https://github.com/microsoft/aspire/blob/8cc1c3e68fd0173dd403692d9930b7f3adf4db99/src/Aspire.Cli/Commands/SettingsSchemaBuilder.cs#L138-L141
https://github.com/dotnet/orleans/blob/30137dd94fb76487cb2b76d3ce049d36acfcee8e/src/Orleans.Serialization/TypeSystem/TypeConverter.cs#L229-L246
https://github.com/reactiveui/refit/blob/92ae878250798dc103a01304704a99bb8033ed77/src/Refit.Reflection/RequestBuilderImplementation.RequestBuilding.cs#L233-L242
https://github.com/NLog/NLog/blob/3c7fd0df1525bfa982b512f7646f2d2b24129391/src/NLog/Internal/ObjectReflectionCache.cs#L636-L661
dotnet-policy-service commentedon Oct 28, 2026
Tagging subscribers to 'linkable-framework': @eerhardt, @vitek-karas, @LakshanF, @sbomer, @joperezr
See info in area-owners.md if you want to be subscribed.
dotnet-policy-service commentedon Oct 28, 2026
Tagging subscribers to this area: @steveisok, @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.
MichalStrehovsky commentedon Oct 28, 2026
I'll flip this to ready for review, I think the case for this is solid enough.
bartonjs commentedon Nov 3, 2026
Video
interfaceType, so let's match.Manish-1kumar commentedon Nov 5, 2026
I'd like to take this one. My plan is to follow the
GetMemberWithSameMetadataDefinitionAspattern: a virtual onTypethat filtersGetInterfaces()withHasSameMetadataDefinitionAs, with anUnconditionalSuppressMessagejustified the same way asImplementInterface(ifinterfaceTypeexists, the trimmer kept the matching interface implementations).A few behaviors the approval doesn't spell out, so I'll go with these unless you'd prefer otherwise:
interfaceTypecan be a generic type definition, a constructed type, or a non-generic interface; all instantiations of the same definition match (so passingIList<int>also returnsIList<string>if implemented).nullthrowsArgumentNullException; a non-interfaceinterfaceTypethrowsArgumentException.agocke commentedon Nov 5, 2026
Why? Seems like we could throw in that case.
Manish-1kumar commentedon Nov 5, 2026
Agreed, there's no need to accept them. I've updated #137016 so that a constructed generic interface throws
ArgumentException.interfaceTypemust be a non-generic interface or a generic interface type definition.Add Type.GetInterfacesWithSameMetadataDefinitionAs (dotnet#137016)