Repository navigation
Closed
Labels
area-System.Runtime.InteropServicessource-generatorIndicates an issue with a source generator feature
Description
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();
}Reacted by Copilot
Metadata
Metadata
Assignees
Labels
area-System.Runtime.InteropServicessource-generatorIndicates an issue with a source generator feature
Type
Projects
- StatusShow more project fieldsNo status
Activity
AaronRobinsonMSFT commentedon Dec 4, 2025
/cc @jkoritzinsky @jtschuster
dotnet-policy-service commentedon Dec 4, 2025
Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.
MihuBot commentedon Dec 4, 2025
I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):