diff --git a/ICD.Common.Utils/EnumUtils.cs b/ICD.Common.Utils/EnumUtils.cs index 30a37e1..ee3bdc2 100644 --- a/ICD.Common.Utils/EnumUtils.cs +++ b/ICD.Common.Utils/EnumUtils.cs @@ -126,10 +126,14 @@ namespace ICD.Common.Utils throw new ArgumentNullException("type"); // Reflection is slow and this method is called a lot, so we cache the results. - if (!s_EnumValuesCache.ContainsKey(type)) - s_EnumValuesCache[type] = GetValuesUncached(type).ToArray(); + object[] cache; + if (!s_EnumValuesCache.TryGetValue(type, out cache)) + { + cache = GetValuesUncached(type).ToArray(); + s_EnumValuesCache[type] = cache; + } - return s_EnumValuesCache[type]; + return cache; } /// @@ -280,13 +284,23 @@ namespace ICD.Common.Utils Type type = typeof(T); - if (!s_EnumFlagsCache.ContainsKey(type)) - s_EnumFlagsCache.Add(type, new Dictionary()); + Dictionary cache; + if (!s_EnumFlagsCache.TryGetValue(type, out cache)) + { + cache = new Dictionary(); + s_EnumFlagsCache[type] = cache; + } - if (!s_EnumFlagsCache[type].ContainsKey(value)) - s_EnumFlagsCache[type][value] = GetValues().Where(e => HasFlag(value, e)).Cast().ToArray(); + object[] flags; + if (!cache.TryGetValue(value, out flags)) + { + flags = GetValues().Where(e => HasFlag(value, e)) + .Cast() + .ToArray(); + cache[value] = flags; + } - return s_EnumFlagsCache[type][value].Cast(); + return flags.Cast(); } ///