I'm trying to compare the hash output of two methods and see if they are equal.
This is my code so far:
Helper.cs:
public static bool HashIsTheSame<TResult>(Func<object[], TResult> method1, Func<object[], TResult> method2, params object[] args)
{
TResult method1Result = method1(args);
TResult method2Result = method2(args);
return GetHashString(method1Result).Equals(GetHashString(method2Result));
}
private static string GetHashString(object inputString)
{
StringBuilder sb = new StringBuilder();
foreach (byte b in GetHash(inputString.ToString()))
sb.Append(b.ToString("X2"));
return sb.ToString();
}
private static byte[] GetHash(string inputString)
{
HashAlgorithm algorithm = SHA256.Create();
return algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString));
}
repo.cs:
public int TestMethod1(int value1, int value2)
{
return value1 + value2;
}
public int TestMethod2(int value1, int value2)
{
return value1 * value2;
}
When I call it like this:
Test.cs:
public void HashTest()
{
var value1 = 1;
var value2 = 2;
object[] args = {value1, value2};
Assert.IsFalse(TestHelper.HashIsTheSame(repo.TestMethod1(value1, value2), repo.TestMethod2(value1, value2), args));
}
I get the error message: "The type arguments for method cannot be inferred from the usage. Try specifying the type arguments explicitly." What am I doing wrong?