Skip to content

Property support in [GeneratedComInterface] interfaces #96502

Closed

Description

@jkoritzinsky

In MIDL, developers can define methods that refer to properties in COM by attributing methods with particular signatures with the propget, propput, or propputref attributes. In built-in COM marshalling, .NET properties defined on a ComVisible(true) interface are turned into two slots on a vtable, a propget getter method and a propput setter method. For ComImport types imported with TLBImp, .NET properties are synthesized for propget and propput or propputref methods.

Note

If the property type is object, TLBs generated for a built-in .NET interface generate a propputref attribute instead of a propput attribute.

Currently, we block GeneratedComInterface interfaces from defining properties. We should look at providing support for properties in a similar way to the built-in .NET support for IUnknown-based interfaces.

We should consider introducing a feature to support specifying that particular methods provide the getter/setter implementation for a property as there is no restriction in COM that the propget and propput methods are sequential in the interface declaration (TLBImp supports defining a property that points to non-sequentially defined methods, but C# doesn't have a mechanism for it). Such a mechanism could be as simple as "define a default implementation of the property that calls the methods" and teach the COM source generator to ignore methods/properties that have default implementations.

API update.

In order to accomodate this effort naturally, we are also proposing the expansion of the MarshalUsingAttribute type to be applied to properties.

- [System.AttributeUsageAttribute(System.AttributeTargets.Parameter | System.AttributeTargets.ReturnValue, AllowMultiple = true)]
+ [System.AttributeUsageAttribute(System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue, AllowMultiple = true)]
public sealed partial class MarshalUsingAttribute : System.Attribute
{
}

This will allow the application of a MarshalUsing in the following scenario.

partial interface INativeAPI
{
  [MarshalUsing(typeof(TrackedIntMarshaller))]
  int Value { get; set; }
}

Contributes to #66674

Activity

  1. added
    source-generator
    on Jan 4, 2024
  2. ghost added
    untriaged
    on Jan 4, 2024
  3. removed
    untriaged
    on Jan 8, 2024
  4. added this to the 9.0.0 milestone on Jan 8, 2024
  5. modified the milestones: 9.0.0, 10.0.0 on Jul 24, 2024
  6. StarRiver123 commented on Jan 12, 2025

    @StarRiver123

    Does it Works in AOT?

  7. m-celikba commented on Jan 23, 2025

    @m-celikba

    @jkoritzinsky is there an ETA for the property support ?
    At least for propget and propput this is something that is vastly used when wrapping native COM object.
    Not having property support is a breaking change when migrating to dotnet.

    I found out that I can just mimic what's done on the C++ side.

    This works:

    [GeneratedComInterface, Guid("..)]
    public partial interface IMyItf
    {
        double get_Value(); 
        void set_Value(double d);
    }
    

    This does not work:

    [GeneratedComInterface, Guid("..)]
    public partial interface IMyItf
    {
        double Value { get; set; }
    }
    

    If we had extension properties in c#, then this will be easier to fix.

  8. modified the milestones: 10.0.0, 11.0.0 on Aug 11, 2025
  9. m-celikba commented on Sep 19, 2025

    @m-celikba

    FYI: I've had success with this using extension properties from c#14 in dotnet10.

    [GeneratedComInterface, Guid("..)]
    public partial interface IMyItf
    {
        double get_Value(); 
        void set_Value(double d);
    }
    
     public static class IMyItfExtensions
     {
         extension(IMyItf arg)
         {
             public double Value
             {
                 get => arg.get_Value();
                 set => arg.set_Value(value);
             }
         }
     }
    

    This provides portable code when migrating from dotnet framework builtin interop.

    The only thing missing are indexers because the feature didn't make it to c#14.
    Indexers appear when using IDispatch interfaces that expose collections.

    example
    // idl file

    [object, uuid(..), dual, pointer_default(unique)]
    interface IMyObjCollection : IDispatch
    {
      [propget, id(DISPID_VALUE)] HRESULT Item([in] int index, [out, retval] IMyObj **ppVal);  
      [propput, id(DISPID_VALUE)] HRESULT Item([in] int index, [in]IMyObj pdVal);
    }
    

    // c# interop

    [GeneratedComInterface, Guid("...")]
    public partial interface IMyObjCollection : IDispatch
    {
        IMyObj get_Item(int index);
        void set_Item(int index, IMyObj newVal);
    }
    

    usage should be (because that's how it was in net framework):

    IMyObjCollection coll = ...;
    coll[1] = ...; // call set_Item using indexer
    Console.WriteLine($"{coll[1]}");  // call get_Item using indexer
    

    @jkoritzinsky @jtschuster @AaronRobinsonMSFT

  10. dotnet-policy-service commented on Apr 13, 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.

  11. dotnet-policy-service commented on May 28, 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.

  12. added
    in-pr
    on Jun 3, 2026
  13. added
    api-approved
    on Jun 15, 2026
  14. AaronRobinsonMSFT commented on Jun 15, 2026

    @AaronRobinsonMSFT
    Member

    The expansion of support to properties of the MarshalUsing attribute was approved offline.

    /cc @bartonjs

  15. locked and limited conversation to collaborators on Jul 18, 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.Runtime.InteropServicesin-prsource-generator

    Type

    No type

    Projects

    • Status
      No status

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions