diff --git a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComClassGenerator.cs b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComClassGenerator.cs index f2cab6db31b2b5..1455804b0ba9db 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComClassGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComClassGenerator.cs @@ -55,7 +55,9 @@ public void Initialize(IncrementalGeneratorInitializationContext context) writer.WriteLine(classInfoType.ToFullString()); writer.WriteLine(); writer.WriteLine(attribute); - context.AddSource(className, writer.ToString()); + // Replace < and > with { and } to make valid hint names for generic types + string hintName = className.Replace('<', '{').Replace('>', '}'); + context.AddSource(hintName, writer.ToString()); }); } diff --git a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/ComClassGeneratorOutputShape.cs b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/ComClassGeneratorOutputShape.cs index cc3dc85bbe5e46..c00ff13aae59f3 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/ComClassGeneratorOutputShape.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/ComClassGeneratorOutputShape.cs @@ -68,6 +68,27 @@ partial class E : C await VerifySourceGeneratorAsync(source, "C", "D", "E"); } + [Fact] + public async Task GenericComClass() + { + string source = """ + using System.Runtime.InteropServices; + using System.Runtime.InteropServices.Marshalling; + + [GeneratedComInterface] + partial interface INativeAPI + { + } + + [GeneratedComClass] + partial class GenericClass : INativeAPI where T : class, new() + { + } + """; + + await VerifySourceGeneratorAsync(source, "GenericClass`1"); + } + private static async Task VerifySourceGeneratorAsync(string source, params string[] typeNames) { GeneratedShapeTest test = new(typeNames)