diff --git a/ICD.Common.Utils/AttributeUtils.cs b/ICD.Common.Utils/AttributeUtils.cs index 48a3c54..79c0a4b 100644 --- a/ICD.Common.Utils/AttributeUtils.cs +++ b/ICD.Common.Utils/AttributeUtils.cs @@ -27,6 +27,8 @@ namespace ICD.Common.Utils private static readonly Dictionary s_AttributeToMethodCache; private static readonly Dictionary> s_TypeToAttributesCache; + private static ILoggerService Logger { get { return ServiceProvider.TryGetService(); } } + /// /// Constructor. /// @@ -58,33 +60,52 @@ namespace ICD.Common.Utils /// Pre-emptively caches the given assembly for lookup. /// /// - public static void CacheAssembly(Assembly assembly) + public static bool CacheAssembly(Assembly assembly) { if (assembly == null) throw new ArgumentNullException("assembly"); if (s_CachedAssemblies.Contains(assembly)) - return; - s_CachedAssemblies.Add(assembly); + return true; #if SIMPLSHARP - CType[] types = new CType[0]; + CType[] types; #else - Type[] types = new Type[0]; + Type[] types; #endif try { types = assembly.GetTypes(); } +#if STANDARD + catch (ReflectionTypeLoadException e) + { + foreach (Exception inner in e.LoaderExceptions) + { + Logger.AddEntry(eSeverity.Error, inner, "{0} failed to cache assembly {1}", typeof(AttributeUtils).Name, + assembly.GetName().Name); + } + + return false; + } +#endif catch (TypeLoadException e) { - ServiceProvider.TryGetService() - .AddEntry(eSeverity.Error, e, "Failed to cache assembly {0} - {1}", assembly.GetName().Name, - e.Message); +#if SIMPLSHARP + Logger.AddEntry(eSeverity.Error, e, "{0} failed to cache assembly {1}", typeof(AttributeUtils).Name, + assembly.GetName().Name); +#else + Logger.AddEntry(eSeverity.Error, e, "{0} failed to cache assembly {1} - could not load type {2}", + typeof(AttributeUtils).Name, assembly.GetName().Name, e.TypeName); +#endif + return false; } foreach (var type in types) CacheType(type); + + s_CachedAssemblies.Add(assembly); + return true; } /// @@ -145,9 +166,9 @@ namespace ICD.Common.Utils s_AttributeToMethodCache[attribute] = method; } - #endregion +#endregion - #region Lookup +#region Lookup /// /// Gets the first attribute on the given class type matching the generic type. @@ -223,6 +244,6 @@ namespace ICD.Common.Utils return s_AttributeToMethodCache[attribute]; } - #endregion +#endregion } }