From a7aa1689c7ee1964e8f102a9160ece193718ea34 Mon Sep 17 00:00:00 2001 From: David Vreony Date: Fri, 14 Jun 2019 06:24:19 +0100 Subject: [PATCH] Fix android bitmap skip dynamic and handle exceptions (#337) --- .../Android/Bitmaps/PlatformBitmapLoader.cs | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/src/Splat/Platforms/Android/Bitmaps/PlatformBitmapLoader.cs b/src/Splat/Platforms/Android/Bitmaps/PlatformBitmapLoader.cs index 9a6444c34..75c48a89e 100644 --- a/src/Splat/Platforms/Android/Bitmaps/PlatformBitmapLoader.cs +++ b/src/Splat/Platforms/Android/Bitmaps/PlatformBitmapLoader.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Reflection; using System.Threading.Tasks; using Android.App; using Android.Graphics; @@ -19,12 +20,14 @@ namespace Splat public class PlatformBitmapLoader : IBitmapLoader { private static readonly Dictionary _drawableList; + private static readonly IFullLogger _log; /// /// Initializes static members of the class. /// static PlatformBitmapLoader() { + _log = Locator.Current.GetService().GetLogger(typeof(PlatformBitmapLoader)); _drawableList = GetDrawableList(); } @@ -99,19 +102,17 @@ public IBitmap Create(float width, float height) internal static Dictionary GetDrawableList() { - var log = Splat.LogHost.Default; - // VS2019 onward var assemblies = AppDomain.CurrentDomain.GetAssemblies() - .SelectMany(x => x.GetTypes()) + .SelectMany(GetTypesFromAssembly) .Where(x => x.Name == "Resource" && x.GetNestedType("Drawable") != null) .Select(x => x.GetNestedType("Drawable")) .ToArray(); - log.Debug(() => "DrawableList. Got " + assemblies.Length + " assemblies."); + _log.Debug(() => "DrawableList. Got " + assemblies.Length + " assemblies."); foreach (var assembly in assemblies) { - log.Debug(() => "DrawableList Assembly: " + assembly.Name); + _log.Debug(() => "DrawableList Assembly: " + assembly.Name); } var result = assemblies @@ -119,13 +120,36 @@ internal static Dictionary GetDrawableList() .Where(x => x.FieldType == typeof(int) && x.IsLiteral) .ToDictionary(k => k.Name, v => (int)v.GetRawConstantValue()); - log.Debug(() => "DrawableList. Got " + result.Count + " items."); + _log.Debug(() => "DrawableList. Got " + result.Count + " items."); foreach (var keyValuePair in result) { - log.Debug(() => "DrawableList Item: " + keyValuePair.Key); + _log.Debug(() => "DrawableList Item: " + keyValuePair.Key); } return result; } + + internal static Type[] GetTypesFromAssembly(Assembly assembly) + { + try + { + return assembly.GetTypes(); + } + catch (ReflectionTypeLoadException e) + { + // The array returned by the Types property of this exception contains a Type + // object for each type that was loaded and null for each type that could not + // be loaded, while the LoaderExceptions property contains an exception for + // each type that could not be loaded. + _log.Warn(e, "Exception while detecting drawing types."); + + foreach (var loaderException in e.LoaderExceptions) + { + _log.Warn(loaderException, "Inner Exception for detecting drawing types."); + } + + return e.Types.Where(x => x != null).ToArray(); + } + } } }