From 51c2e3dcde7b91410ddda414d966defc71e8a728 Mon Sep 17 00:00:00 2001 From: David Naylor <dave.naylor@gmail.com> Date: Thu, 28 Jul 2022 00:15:31 -0700 Subject: [PATCH] Use reference equality for Proxy cache key --- src/DivertR/ViaBuilderExtensions.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/DivertR/ViaBuilderExtensions.cs b/src/DivertR/ViaBuilderExtensions.cs index e5af5f82..03743e87 100644 --- a/src/DivertR/ViaBuilderExtensions.cs +++ b/src/DivertR/ViaBuilderExtensions.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Runtime.CompilerServices; namespace DivertR { @@ -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) @@ -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); + } + } } } \ No newline at end of file