diff --git a/src/WinRT.Runtime/TypeExtensions.cs b/src/WinRT.Runtime/TypeExtensions.cs index b489feb39..de4716158 100644 --- a/src/WinRT.Runtime/TypeExtensions.cs +++ b/src/WinRT.Runtime/TypeExtensions.cs @@ -1,35 +1,38 @@ using System; -using System.Collections.Generic; -using System.Diagnostics; +using System.Collections.Concurrent; using System.Reflection; -using System.Text; namespace WinRT { public static class TypeExtensions { + private readonly static ConcurrentDictionary HelperTypeCache = new ConcurrentDictionary(); + public static Type FindHelperType(this Type type) { - if (typeof(Exception).IsAssignableFrom(type)) - { - type = typeof(Exception); - } - Type customMapping = Projections.FindCustomHelperTypeMapping(type); - if (customMapping is object) - { - return customMapping; - } - - string fullTypeName = type.FullName; - string ccwTypePrefix = "ABI.Impl."; - if (fullTypeName.StartsWith(ccwTypePrefix)) - { - fullTypeName = fullTypeName.Substring(ccwTypePrefix.Length); - } - - var helper = $"ABI.{fullTypeName}"; - return Type.GetType(helper) ?? type.Assembly.GetType(helper); + return HelperTypeCache.GetOrAdd(type, (type) => + { + if (typeof(Exception).IsAssignableFrom(type)) + { + type = typeof(Exception); + } + Type customMapping = Projections.FindCustomHelperTypeMapping(type); + if (customMapping is object) + { + return customMapping; + } + + string fullTypeName = type.FullName; + string ccwTypePrefix = "ABI.Impl."; + if (fullTypeName.StartsWith(ccwTypePrefix)) + { + fullTypeName = fullTypeName.Substring(ccwTypePrefix.Length); + } + + var helper = $"ABI.{fullTypeName}"; + return Type.GetType(helper) ?? type.Assembly.GetType(helper); + }); } public static Type GetHelperType(this Type type)