From 1f495bfd0edf1509c3844cc04e074203ad716760 Mon Sep 17 00:00:00 2001 From: Marc Hernandez Date: Sat, 16 Jan 2021 18:15:31 -0800 Subject: [PATCH] x) Fix multiple attempts to fill in the type cache --- reflect/refl.cs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/reflect/refl.cs b/reflect/refl.cs index d4101d2..1e2eacb 100644 --- a/reflect/refl.cs +++ b/reflect/refl.cs @@ -148,18 +148,26 @@ static public class refl public static ImmutableList GetAllFields( Type t ) { - if( s_fieldCache.TryGetValue( t, out var info ) ) - return info; + { + if( s_fieldCache.TryGetValue( t, out var first ) ) + return first; + } - var list = new List(); + lock( t ) + { + if( s_fieldCache.TryGetValue( t, out var second ) ) + return second; - GetAllFields( t, list ); + var list = new List(); - var immList = list.ToImmutableList(); + GetAllFields( t, list ); - Interlocked.Exchange( ref s_fieldCache, s_fieldCache.Add( t, immList ) ); + var immList = list.ToImmutableList(); - return immList; + Interlocked.Exchange( ref s_fieldCache, s_fieldCache.Add( t, immList ) ); + + return immList; + } }