From e7224f22575ec26ae89a6a9750303ef7d35c20ed Mon Sep 17 00:00:00 2001 From: Simon McKenzie <17579442+simonmckenzie@users.noreply.github.com> Date: Wed, 14 Aug 2024 10:17:46 +1000 Subject: [PATCH] Handle generic types in method deduplication This fixes an error where methods with the same parameters but different _generic type parameters_ were being treated as identical during deduplication, resulting in the loss of interface methods. The fix is to use `genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters` when constucting the `SymbolDisplayFormat` used for deduplication. Before: `public void AMethod(Func getValue) {}` produced a deduplication signature of `AMethod(Func)` After: `public void AMethod(Func getValue) {}` produced a deduplication signature of `AMethod(Func)` --- .../AutomaticInterface/Builder.cs | 1 + AutomaticInterface/Tests/GeneratorTests.cs | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/AutomaticInterface/AutomaticInterface/Builder.cs b/AutomaticInterface/AutomaticInterface/Builder.cs index b97e564..303c07a 100644 --- a/AutomaticInterface/AutomaticInterface/Builder.cs +++ b/AutomaticInterface/AutomaticInterface/Builder.cs @@ -13,6 +13,7 @@ public static class Builder private static readonly SymbolDisplayFormat MethodSignatureDisplayFormat = new( + genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters, memberOptions: SymbolDisplayMemberOptions.IncludeParameters, parameterOptions: SymbolDisplayParameterOptions.IncludeType | SymbolDisplayParameterOptions.IncludeParamsRefOut diff --git a/AutomaticInterface/Tests/GeneratorTests.cs b/AutomaticInterface/Tests/GeneratorTests.cs index b548d97..ea312e2 100644 --- a/AutomaticInterface/Tests/GeneratorTests.cs +++ b/AutomaticInterface/Tests/GeneratorTests.cs @@ -2031,6 +2031,55 @@ public partial interface IDemoClass GenerateCode(code).Should().Be(expected); } + [Fact] + public void WorksWithGenericParameterOverloads() + { + const string code = """ + + using AutomaticInterface; + + namespace AutomaticInterfaceExample; + + [GenerateAutomaticInterface] + public class DemoClass + { + public void AMethod(Func getValue) {} + + public void AMethod(Func getValue) {} + } + + """; + + const string expected = """ + //-------------------------------------------------------------------------------------------------- + // + // This code was generated by a tool. + // + // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. + // + //-------------------------------------------------------------------------------------------------- + + using System.CodeDom.Compiler; + using AutomaticInterface; + + namespace AutomaticInterfaceExample + { + [GeneratedCode("AutomaticInterface", "")] + public partial interface IDemoClass + { + /// + void AMethod(Func getValue); + + /// + void AMethod(Func getValue); + + } + } + + """; + GenerateCode(code).Should().Be(expected); + } + [Fact] public void WorksWithEventOverrides() {