Skip to content

[API Proposal]: UnmanagedCallersOnlyAttribute.AssociatedSourceType #127634

Closed

Description

@MichalStrehovsky

Background and motivation

UnmanagedCallersOnlyAttribute has an EntryPoint property that specifies the method should be available externally under the specified symbolic names. Native AOT and tools like DNNE use this to generate the exports.

This export is unconditonal. If a method in the assembly has this attribute, and the assembly is looked at by the generating tool, the export will be generated and the implementation rooted.

This can be problematic for pregenerated interop code. For example in interop on Apple platforms, the user can specify that their class should be available from native code:

public class MyObject : NSObject
{
    [Export("myMethod")]
    void MyMethod() {}
}

The interop code will generate a native-callable proxy behind the scenes:

class MyObjectProxy
{
    [UnmanagedCallersOnly(EntryPoint = "MyObjectProxy_MyMethod"]
    static void MyMethod(IntPtr handle)
    {
        var obj = (MyObject)Runtime.GetNSObject(handle);
        obj.MyMethod();
    }
}

The proxy entrypoint is generated with a specified EntryPoint because further native glue code will refer to it.

The proxy entrypoint is however not needed if the MyObject class was never used in the program.

#127575 was asking for a mechanism that would allow specifying a list of EntryPoints to generate. However creating such list requires analyzing the program before native compilation or trimming happens. It would be preferable to connect trimming with the UnmanagedCallersOnly EntryPoint generation logic so that we don't have to effectively run trimming twice.

The subsequent downstream tools can then observe entrypoints that survivied trimming either by inspecting the attribute metadata table in IL in case of IL trimming, or by inspecting the symbols or linker scripts in case of native compilation, or through a custom protocol if inspecting normal compilation outputs is undesirable.

API Proposal

namespace System.Runtime.InteropServices;

public partial class UnmanagedCallersOnlyAttribute : Attribute
{
    public Type? AssociatedSourceType;
}

API Usage

class MyObjectProxy
{
    [UnmanagedCallersOnly(EntryPoint = "MyObjectProxy_MyMethod", AssociatedSourceType = typeof(MyObject))]
    static void MyMethod(IntPtr handle)
    {
        var obj = (MyObject)Runtime.GetNSObject(handle);
        obj.MyMethod();
    }
}

Alternative Designs

Risks

AssociatedSourceType with the rule of "if the an object of this type could exist in the program" may not be sufficiently flexible and additional rules could be needed (for example, what if someone wants this to be generate if some other method was compiled/kept). There are no known uses cases beyond this one type-based rule right now though.

Activity

  1. added this to the 11.0.0 milestone on May 1, 2026
  2. added
    api-suggestion
    on May 1, 2026
  3. dotnet-policy-service commented on May 1, 2026

    @dotnet-policy-service
    Contributor

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

  4. jkoritzinsky commented on May 1, 2026

    @jkoritzinsky
    Member

    If the semantics are more similar to the associated/proxy type map, could we name the property AssociatedType/AssociatedTrimType/AssociatedTypeRoot or something of the like and apply the same rules as the associated/proxy type map? I'd rather avoid having 3 sets of trimming rules if 2 will get us what we need.

  5. changed the title [-][API Proposal]: UnmanagedCallersOnlyAttribute.TrimTarget[/-] [+][API Proposal]: UnmanagedCallersOnlyAttribute.AssociatedType[/+] on May 8, 2026
  6. MichalStrehovsky commented on May 8, 2026

    @MichalStrehovsky
    MemberAuthor

    If the semantics are more similar to the associated/proxy type map, could we name the property AssociatedType/AssociatedTrimType/AssociatedTypeRoot or something of the like and apply the same rules as the associated/proxy type map? I'd rather avoid having 3 sets of trimming rules if 2 will get us what we need.

    Sounds good, updated to AssociatedType!

  7. added
    api-ready-for-review
    blocking
    and removed
    api-suggestion
    on May 8, 2026
  8. MichalStrehovsky commented on May 8, 2026

    @MichalStrehovsky
    MemberAuthor

    I marked ready for review and also blocking to enable consumption in macios soon.

  9. MichalStrehovsky commented on May 8, 2026

    @MichalStrehovsky
    MemberAuthor

    Cc @dotnet/illink too

  10. AaronRobinsonMSFT commented on May 12, 2026

    @AaronRobinsonMSFT
    Member
  11. AaronRobinsonMSFT commented on May 12, 2026

    @AaronRobinsonMSFT
    Member

    The AssociatedType field name is inspired by the TypeMapAssociationAttribute<>.

  12. AaronRobinsonMSFT commented on May 12, 2026

    @AaronRobinsonMSFT
    Member

    Another field name option is AssociatedSourceType. This matches the terminology in the constructor, TypeMapAssociationAttribute(Type source, Type proxy), where the source represents the Type that determines if the proxy Type is kept. In this case, the source is the Type and the equivalent "proxy" is the UnmanagedCallersOnly that will be trimmed if source isn't referenced.

  13. bartonjs commented on May 12, 2026

    @bartonjs
    Member

    Video

    • There was discussion as to how to handle a callback that was based on two types, and the feeling was that it can be handled with typemap.
      • We could add a Type[] AssociatedTypes later if need be.
    • Added infix "Source" to match the name in typemap.
    • Associated{Source}Type should really be a property, but all of the other members on this attribute type are fields, so this one can be a field, too.
    namespace System.Runtime.InteropServices;
    
    public partial class UnmanagedCallersOnlyAttribute : Attribute
    {
        public Type? AssociatedSourceType;
    }
  14. added
    api-approved
    and removed
    blocking
    api-ready-for-review
    on May 12, 2026
  15. changed the title [-][API Proposal]: UnmanagedCallersOnlyAttribute.AssociatedType[/-] [+][API Proposal]: UnmanagedCallersOnlyAttribute.AssociatedSourceType[/+] on May 14, 2026
  16. added a commit that references this issue on Jun 3, 2026
    2165f14
  17. locked and limited conversation to collaborators on Jul 4, 2026
  18. added a commit that references this issue on Jul 15, 2026
    e324b81
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

api-approvedarea-System.Runtime.InteropServices

Type

No type

Projects

  • Status
    No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions