Skip to content

Commit

Permalink
Merge pull request #36 from jeffska/cacheable-inv-fix
Browse files Browse the repository at this point in the history
Fix equality and hash code of CacheableInvocation
  • Loading branch information
jbtule authored Nov 30, 2022
2 parents a345a04 + 9f3bbaf commit c6a4202
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Dynamitey/CacheableInvocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using Microsoft.CSharp.RuntimeBinder;
using System.Reflection;
using Dynamitey.Internal.Compat;
using System.Collections;
using System.Collections.Generic;

namespace Dynamitey
{
Expand Down Expand Up @@ -167,7 +169,7 @@ public bool Equals(CacheableInvocation other)
if (ReferenceEquals(this, other)) return true;
return base.Equals(other)
&& other._argCount == _argCount
&& Equals(other._argNames, _argNames)
&& (_argNames ?? new string[] { }).SequenceEqual(other._argNames ?? new string[] { })
&& other._staticContext.Equals(_staticContext)
&& Equals(other._context, _context)
&& other._convertExplicit.Equals(_convertExplicit)
Expand Down Expand Up @@ -200,7 +202,7 @@ public override int GetHashCode()
{
int result = base.GetHashCode();
result = (result*397) ^ _argCount;
result = (result*397) ^ (_argNames != null ? _argNames.GetHashCode() : 0);
result = (result*397) ^ (_argNames != null ? ((IStructuralEquatable)_argNames).GetHashCode(EqualityComparer<string>.Default) : 0);
result = (result*397) ^ _staticContext.GetHashCode();
result = (result*397) ^ (_context != null ? _context.GetHashCode() : 0);
result = (result*397) ^ _convertExplicit.GetHashCode();
Expand Down
11 changes: 11 additions & 0 deletions Tests/Invoke.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1365,7 +1365,18 @@ public void TestDynamicMemberNamesImpromput()
Assert.AreEqual("Two", Dynamic.GetMemberNames(tDict, dynamicOnly: true).Single());
}

[Test]
public void TestCachedInvocationEquality()
{
var tCachedIvnocation1 = new CacheableInvocation(InvocationKind.InvokeMember, "Func", argCount: 2,
argNames: new[] { "two" });

var tCachedIvnocation2 = new CacheableInvocation(InvocationKind.InvokeMember, "Func", argCount: 2,
argNames: new[] { "two" });

Assert.AreEqual(tCachedIvnocation1.GetHashCode(), tCachedIvnocation2.GetHashCode());
Assert.AreEqual(tCachedIvnocation1, tCachedIvnocation2);
}


private DynamicObject CreateMock(ExpressionType op)
Expand Down

0 comments on commit c6a4202

Please sign in to comment.