Skip to content

COM class source generator fails awkwardly with type generics #122191

Closed

Description

@AaronRobinsonMSFT

When generating a COM class with the COM source generator, the following behavior results. The warneing does indicate some issue, but it is odd it fails for both the generic and non-generic version.

The build has a warning that does indicate the problem, but the run-time behavior is rather confusing.

CSC : warning CS8785: Generator 'ComClassGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'ArgumentException' with message 'The hintName 'ClassFactory<T>' contains an invalid character '<' at position 12. (Parameter 'hintName')'.

Repro:

// > dotnet run
// CSC : warning CS8785: Generator 'ComClassGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'ArgumentException' with message 'The hintName 'ClassFactory<T>' contains an invalid character '<' at position 12. (Parameter 'hintName')'.
// Failed to query for IClassFactory: -2147467262
// Failed to query for IClassFactory: -2147467262

using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

QueryForClassFactory(new ClassFactory());
QueryForClassFactory(new ClassFactory<object>());

static unsafe void QueryForClassFactory<T>(T t) where T : class, new()
{
    T factory = new();
    nint factoryPtr = (nint)ComInterfaceMarshaller<T>.ConvertToUnmanaged(factory);
    Guid IID_IClassFactory = typeof(IClassFactory).GUID;
    var hr = Marshal.QueryInterface(factoryPtr, in IID_IClassFactory, out nint ppvObject);
    Marshal.Release(factoryPtr);
    if (hr != 0)
    {
        Console.WriteLine($"Failed to query for IClassFactory: {hr}");
        return;
    }

    Console.WriteLine("Successfully queried for IClassFactory.");
    Marshal.Release(ppvObject);
}

[GeneratedComInterface]
[Guid("00000001-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal partial interface IClassFactory
{
    void CreateInstance(
        [MarshalAs(UnmanagedType.Interface)] object? pUnkOuter,
        ref Guid riid,
        out IntPtr ppvObject);

    void LockServer([MarshalAs(UnmanagedType.Bool)] bool fLock);
}

[GeneratedComClass]
internal partial class ClassFactory : IClassFactory
{
   public unsafe void CreateInstance([MarshalAs(UnmanagedType.Interface)] object? pUnkOuter, ref Guid riid, out nint ppvObject)
    => throw new NotImplementedException();

   public void LockServer([MarshalAs(UnmanagedType.Bool)] bool fLock)
    => throw new NotImplementedException();
}

[GeneratedComClass]
internal partial class ClassFactory<T> : IClassFactory where T : class, new()
{
   public unsafe void CreateInstance([MarshalAs(UnmanagedType.Interface)] object? pUnkOuter, ref Guid riid, out nint ppvObject)
    => throw new NotImplementedException();

   public void LockServer([MarshalAs(UnmanagedType.Bool)] bool fLock)
    => throw new NotImplementedException();
}

Activity

  1. added
    untriaged
    on Dec 4, 2025
  2. AaronRobinsonMSFT commented on Dec 4, 2025

    @AaronRobinsonMSFT
    MemberAuthor
  3. dotnet-policy-service commented on Dec 4, 2025

    @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. MihuBot commented on Dec 4, 2025

    @MihuBot

    I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):

  5. added
    source-generator
    on Dec 4, 2025
  6. removed
    untriaged
    on Dec 5, 2025
  7. locked and limited conversation to collaborators on Jan 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.Runtime.InteropServicessource-generator

Type

No type

Projects

  • Status
    No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions