Skip to content

Commit

Permalink
Merge pull request #158 from PublicApiGenerator/method-param-matching
Browse files Browse the repository at this point in the history
Method param matching
  • Loading branch information
danielmarbach committed Nov 25, 2019
2 parents f0a64bd + 02c13f7 commit a2b5e9c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/PublicApiGenerator/MethodNameBuilder.cs
@@ -1,5 +1,7 @@
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;

namespace PublicApiGenerator
Expand All @@ -18,10 +20,24 @@ public static class MethodNameBuilder

var isNew = methodDefinition.IsNew(typeDef => typeDef?.Methods, e =>
e.Name.Equals(methodDefinition.Name, StringComparison.Ordinal) &&
e.Parameters.Count == methodDefinition.Parameters.Count);
e.Parameters.Count == methodDefinition.Parameters.Count &&
e.Parameters.SequenceEqual(methodDefinition.Parameters, new ParameterTypeComparer()));

return ModifierMarkerNameBuilder.Build(methodDefinition, attributes, isNew, name,
CodeNormalizer.MethodModifierMarkerTemplate);
}

class ParameterTypeComparer : IEqualityComparer<ParameterDefinition>
{
public bool Equals(ParameterDefinition x, ParameterDefinition y)
{
return x?.ParameterType == y?.ParameterType;
}

public int GetHashCode(ParameterDefinition obj)
{
return obj.GetHashCode();
}
}
}
}
28 changes: 28 additions & 0 deletions src/PublicApiGeneratorTests/Method_modifiers.cs
Expand Up @@ -299,6 +299,20 @@ public class ClassWithPublicMethodHiding : PublicApiGeneratorTests.Examples.Clas
}");
}

[Fact]
public void Should_not_output_new_when_base_differs_in_parameters()
{
AssertPublicApi<ClassWithBaseMethodConstraint>(
@"namespace PublicApiGeneratorTests.Examples
{
public class ClassWithBaseMethodConstraint : PublicApiGeneratorTests.Examples.ClassWithBaseMethod
{
public ClassWithBaseMethodConstraint() { }
public void SomeMethod(PublicApiGeneratorTests.Examples.ClassWithBaseMethodConstraint input1, PublicApiGeneratorTests.Examples.ClassWithBaseMethodConstraint input2) { }
}
}");
}

[Fact]
public void Should_output_unsafe_modifier()
{
Expand Down Expand Up @@ -547,6 +561,20 @@ public new ClassWithMethodExtensions<T> Extend(string parameter)
return this;
}
}

public class ClassWithBaseMethod
{
public void SomeMethod(ClassWithBaseMethod input1, ClassWithBaseMethod input2)
{
}
}

public class ClassWithBaseMethodConstraint: ClassWithBaseMethod
{
public void SomeMethod(ClassWithBaseMethodConstraint input1, ClassWithBaseMethodConstraint input2)
{
}
}
}
// ReSharper restore ClassWithVirtualMembersNeverInherited.Global
// ReSharper restore RedundantOverridenMember
Expand Down

0 comments on commit a2b5e9c

Please sign in to comment.