Skip to content

[API Proposal]: Trim-safe Type.GetInterfaces() filtering output based on type definition #136546

Description

@NinoFloris

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.

https://github.com/npgsql/npgsql/blob/b35800b47764b9bb5e0fce3c6d06132ca8107c52/src/Npgsql/Internal/TypeInfoMapping.cs#L779-L810

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:

// 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

Activity

  1. added
    api-suggestion
    on Oct 27, 2026
  2. added
    untriaged
    on Oct 27, 2026
  3. added
    area-Tools-ILLink
    on Oct 27, 2026
  4. dotnet-policy-service commented on Oct 27, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @agocke, @dotnet/illink
    See info in area-owners.md if you want to be subscribed.

  5. teo-tsirpanis commented on Oct 27, 2026

    @teo-tsirpanis
    Contributor

    The trimmer would suppress the RUC warning when there is a statically provided type

    Why not always be trim-safe? Because if you are passing a Type instance to this method, that type must exist in the first place.

    And how about we call it GetInterfacesWithSameMetadataDefinitionAs?

  6. NinoFloris commented on Oct 27, 2026

    @NinoFloris
    ContributorAuthor

    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.

  7. added
    linkable-framework
    and removed
    area-Tools-ILLink
    on Oct 28, 2026
  8. dotnet-policy-service commented on Oct 28, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to 'linkable-framework': @eerhardt, @vitek-karas, @LakshanF, @sbomer, @joperezr
    See info in area-owners.md if you want to be subscribed.

  9. dotnet-policy-service commented on Oct 28, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @steveisok, @dotnet/area-system-reflection
    See info in area-owners.md if you want to be subscribed.

  10. MichalStrehovsky commented on Oct 28, 2026

    @MichalStrehovsky
    Member

    I'll flip this to ready for review, I think the case for this is solid enough.

  11. added
    api-ready-for-review
    and removed
    api-suggestion
    on Oct 28, 2026
  12. bartonjs commented on Nov 3, 2026

    @bartonjs
    Member

    Video

    • The normal name for a Type parameter representing an interface in the Type class is interfaceType, so let's match.
    • GetInterfacesWithSameMetadataDefinitionAs feels wordy, but matches GetMemberWithSameMetadataDefinitionAs, so, it is what it is.
    namespace System;
    
    public partial abstract class Type
    {
        public virtual Type[] GetInterfacesWithSameMetadataDefinitionAs(Type interfaceType);
    }
  13. added
    api-approved
    and removed
    api-ready-for-review
    on Nov 3, 2026
  14. Manish-1kumar commented on Nov 5, 2026

    @Manish-1kumar
    Contributor

    I'd like to take this one. My plan is to follow the GetMemberWithSameMetadataDefinitionAs pattern: a virtual on Type that filters GetInterfaces() with HasSameMetadataDefinitionAs, with an UnconditionalSuppressMessage justified the same way as ImplementInterface (if interfaceType exists, 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:

    • interfaceType can be a generic type definition, a constructed type, or a non-generic interface; all instantiations of the same definition match (so passing IList<int> also returns IList<string> if implemented).
    • null throws ArgumentNullException; a non-interface interfaceType throws ArgumentException.
    • No match returns an empty array.
  15. agocke commented on Nov 5, 2026

    @agocke
    Member

    interfaceType can be a... constructed type

    Why? Seems like we could throw in that case.

  16. Manish-1kumar commented on Nov 5, 2026

    @Manish-1kumar
    Contributor

    Agreed, there's no need to accept them. I've updated #137016 so that a constructed generic interface throws ArgumentException. interfaceType must be a non-generic interface or a generic interface type definition.

  17. removed
    untriaged
    on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-System.Reflectionlinkable-framework

    Type

    No type

    Projects

    • Status
      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions