Skip to content

Instantly share code, notes, and snippets.

@Sl4vP0weR
Created January 2, 2025 13:02
Show Gist options
  • Save Sl4vP0weR/f18c35dfef06932598ae5b582043fd8d to your computer and use it in GitHub Desktop.
Save Sl4vP0weR/f18c35dfef06932598ae5b582043fd8d to your computer and use it in GitHub Desktop.
public class MethodSignatureComparer : IEqualityComparer<ParameterInfo>
{
public static readonly MethodSignatureComparer Default = new();
public bool Match(MethodInfo signatureMethod, MethodInfo otherMethod)
{
var signatureParameters = signatureMethod.GetParameters();
var parameters = otherMethod.GetParameters();
if (otherMethod.Name != signatureMethod.Name)
return false;
if (signatureParameters.Any(x => !parameters.Contains(x, Default)))
return false;
return Equals(otherMethod.ReturnParameter, signatureMethod.ReturnParameter);
}
public bool Equals(ParameterInfo? x, ParameterInfo? y)
{
if (ReferenceEquals(x, y)) return true;
if (x is null) return false;
if (y is null) return false;
if (x.IsOut != y.IsOut) return false;
if (x.IsRetval != y.IsRetval) return false;
if (x.Name != y.Name) return false;
if (x.ParameterType != y.ParameterType) return false;
return true;
}
public int GetHashCode(ParameterInfo obj) =>
HashCode.Combine(obj.IsOut, obj.IsRetval, obj.Name, obj.ParameterType);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment