From c812e3aec92103f9a2f35386e2cbd3a7e541d0dc Mon Sep 17 00:00:00 2001 From: Shannon Date: Fri, 8 Nov 2019 15:31:38 +1100 Subject: [PATCH] fixing tests, reverting change to TypeHelper, just leaving it as is but with caching --- .../Composing/TypeHelper.cs | 56 +++---------------- 1 file changed, 8 insertions(+), 48 deletions(-) diff --git a/src/Umbraco.Abstractions/Composing/TypeHelper.cs b/src/Umbraco.Abstractions/Composing/TypeHelper.cs index 768709c44c..1752521b5d 100644 --- a/src/Umbraco.Abstractions/Composing/TypeHelper.cs +++ b/src/Umbraco.Abstractions/Composing/TypeHelper.cs @@ -19,6 +19,8 @@ namespace Umbraco.Core.Composing = new ConcurrentDictionary, PropertyInfo[]>(); private static readonly ConcurrentDictionary GetFieldsCache = new ConcurrentDictionary(); + private static readonly ConcurrentDictionary TypeNamesCache + = new ConcurrentDictionary(); private static readonly Assembly[] EmptyAssemblies = new Assembly[0]; @@ -29,57 +31,15 @@ namespace Umbraco.Core.Composing /// public static Type GetTypeByName(string name) { + // First try using the basic functionality var type = Type.GetType(name); if (type != null) return type; - // now try fall back procedures. null may be returned because Type.GetName only returns types that have already been loaded in the appdomain - // and this type might not be there yet, so we need to parse the type name and then try to load in via assembly. - - var typeName = TypeName.Parse(name); - try - { - var assembly = Assembly.Load(typeName.AssemblyName); - type = assembly.GetType(typeName.Name); - return type; - } - catch (NotSupportedException) - { - throw; - } - catch (Exception) - { - return null; - } - } - - /// - /// Used to parse a fully qualified type name - /// - /// - /// Does not support generics. - /// This is a simple utility class and is not an exhaustive type name parser (which doesn't exist in .net strangely) - /// - private class TypeName - { - private TypeName(string name, AssemblyName assemblyName) - { - Name = name; - AssemblyName = assemblyName; - } - - public static TypeName Parse(string name) - { - if (name.Contains("[[")) - throw new NotSupportedException($"{nameof(TypeName)} does not support generic types"); - - var index = name.IndexOf(','); - return index > 0 - ? new TypeName(name.Substring(0, index).Trim(), new AssemblyName(name.Substring(index + 1).Trim())) - : new TypeName(name, null); - } - - public string Name { get; } - public AssemblyName AssemblyName { get; } + // It didn't parse, so try loading from each already loaded assembly and cache it + return TypeNamesCache.GetOrAdd(name, s => + AppDomain.CurrentDomain.GetAssemblies() + .Select(x => x.GetType(s)) + .FirstOrDefault(x => x != null)); } ///