Skip to content

Commit

Permalink
Use reference equality for Proxy cache key
Browse files Browse the repository at this point in the history
  • Loading branch information
devodo committed Jul 28, 2022
1 parent 15c32db commit 51c2e3d
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/DivertR/ViaBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace DivertR
{
Expand All @@ -16,7 +18,7 @@ public static IVia<TReturn> RedirectVia<TTarget, TReturn>(this IFuncViaBuilder<T
where TTarget : class
where TReturn : class
{
var proxyCache = new ConcurrentDictionary<object, TReturn>();
var proxyCache = new ConcurrentDictionary<object, TReturn>(new ReferenceEqualityComparer<object>());
var via = viaBuilder.Via.ViaSet.Via<TReturn>(name);

TReturn? RedirectDelegate(IFuncRedirectCall<TTarget, TReturn> call)
Expand All @@ -36,5 +38,18 @@ public static IVia<TReturn> RedirectVia<TTarget, TReturn>(this IFuncViaBuilder<T

return via;
}

private class ReferenceEqualityComparer<T> : IEqualityComparer<T> where T : class
{
public int GetHashCode(T value)
{
return RuntimeHelpers.GetHashCode(value);
}

public bool Equals(T left, T right)
{
return ReferenceEquals(left, right);
}
}
}
}

0 comments on commit 51c2e3d

Please sign in to comment.