Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 14, 2025

The [NativeMarshalling(typeof(CustomMarshaller<T>))] attribute on COM interfaces was ignored, always using the default ComInterfaceMarshaller<T> instead.

[GeneratedComInterface]
[Guid("...")]
[NativeMarshalling(typeof(UniqueComInterfaceMarshaller<IFoo>))]  // Was ignored
partial interface IFoo
{
    void DoWorkTogether(IFoo foo);  // Used ComInterfaceMarshaller<T> instead
}

Changes

  • ComInterfaceMarshallingInfoProvider.cs: Return null when NativeMarshallingAttribute is present, allowing the parser chain to reach NativeMarshallingAttributeParser which handles the custom marshaller.

  • Tests: Added validation for both COM interface method parameters and LibraryImport usage.

This allows consumers to specify custom marshallers (e.g., UniqueComInterfaceMarshaller<T> for unique RCW instances) via the standard NativeMarshallingAttribute mechanism.

Original prompt

This section details on the original issue you should resolve

<issue_title>Interop source generators do not respect [NativeMarshalling(typeof(TMarshaller))] for COM interfaces</issue_title>
<issue_description>In the COM interface generator and LibraryImport generator, we should be able to specify [NativeMarshalling(typeof(UniqueComInterfaceMarshaller<IFoo>))] to instruct the generators to use that marshaller. However, it doesn't override the default marshaller for COM interfaces.

In the example below, the marshalling of foo parameters should use UniqueComInterfaceMarshaller, but use ComInterfaceMarshaller.

[GeneratedComInterface]
[GUID(...)]
[NativeMarshalling(typeof(UniqueComInterfaceMarshaller<IFoo>))]
internal partial interface IFoo
{
    string GetMessage();
    void DoWorkTogether(IFoo foo);
}

public static class PInvokes
{
    [LibraryImport("lib")]
    bool TryGetFoo(out IFoo foo);
}

This shouldn't require changing code outside of src/libraries/System.Runtime.InteropServices.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Modified ComInterfaceMarshallingInfoProvider to check for NativeMarshallingAttribute on COM interfaces and defer to that parser when present. Added tests to verify the fix works for both ComInterfaceGenerator and LibraryImportGenerator.

Co-authored-by: jtschuster <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Interop source generators do not respect [NativeMarshalling(typeof(TMarshaller))] for COM interfaces

2 participants