|
| 1 | +// Copyright (c) 2007-2016 CSJ2K contributors. |
| 2 | +// Licensed under the BSD 3-Clause License. |
| 3 | + |
| 4 | +namespace CSJ2K |
| 5 | +{ |
| 6 | + using System; |
| 7 | + using System.Collections.Generic; |
| 8 | + using System.Linq; |
| 9 | + using System.Reflection; |
| 10 | + |
| 11 | + using CSJ2K.Util; |
| 12 | + |
| 13 | + /// <summary> |
| 14 | + /// Setup helper methods for initializing library. |
| 15 | + /// </summary> |
| 16 | + internal static class J2kSetup |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Gets a single instance from the platform assembly implementing the <typeparamref name="T"/> type. |
| 20 | + /// </summary> |
| 21 | + /// <typeparam name="T">(Abstract) type for which implementation is requested.</typeparam> |
| 22 | + /// <returns>The single instance from the platform assembly implementing the <typeparamref name="T"/> type, |
| 23 | + /// or null if no or more than one implementations are available.</returns> |
| 24 | + /// <remarks>It is implicitly assumed that implementation class has a public, parameter-less constructor.</remarks> |
| 25 | + internal static T GetSinglePlatformInstance<T>() |
| 26 | + { |
| 27 | + try |
| 28 | + { |
| 29 | + var assembly = GetCurrentAssembly(); |
| 30 | +#if NETFX_CORE || NETSTANDARD |
| 31 | + var type = |
| 32 | + assembly.DefinedTypes.Single( |
| 33 | + t => (t.IsSubclassOf(typeof(T)) || typeof(T).GetTypeInfo().IsAssignableFrom(t)) && !t.IsAbstract) |
| 34 | + .AsType(); |
| 35 | +#else |
| 36 | + var type = |
| 37 | + assembly.GetTypes() |
| 38 | + .Single(t => (t.IsSubclassOf(typeof(T)) || typeof(T).IsAssignableFrom(t)) && !t.IsAbstract); |
| 39 | +#endif |
| 40 | + var instance = (T)Activator.CreateInstance(type); |
| 41 | + |
| 42 | + return instance; |
| 43 | + } |
| 44 | + catch (Exception) |
| 45 | + { |
| 46 | + return default(T); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Gets the default classified instance from the platform assembly implementing the <typeparamref name="T"/> type. |
| 52 | + /// </summary> |
| 53 | + /// <typeparam name="T">(Abstract) type for which implementation is requested.</typeparam> |
| 54 | + /// <returns>The single instance from the platform assembly implementing the <typeparamref name="T"/> type that is classified as default, |
| 55 | + /// or null if no or more than one default classified implementations are available.</returns> |
| 56 | + /// <remarks>It is implicitly assumed that all implementation classes has a public, parameter-less constructor.</remarks> |
| 57 | + internal static T GetDefaultPlatformInstance<T>() where T : IDefaultable |
| 58 | + { |
| 59 | + try |
| 60 | + { |
| 61 | + var assembly = GetCurrentAssembly(); |
| 62 | + var types = GetConcreteTypes<T>(assembly); |
| 63 | + |
| 64 | + return GetDefaultOrSingleInstance<T>(types); |
| 65 | + } |
| 66 | + catch (Exception) |
| 67 | + { |
| 68 | + return default(T); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private static Assembly GetCurrentAssembly() |
| 73 | + { |
| 74 | +#if NETFX_CORE || NETSTANDARD |
| 75 | + return typeof(J2kSetup).GetTypeInfo().Assembly; |
| 76 | +#else |
| 77 | + return typeof(J2kSetup).Assembly; |
| 78 | +#endif |
| 79 | + } |
| 80 | + |
| 81 | + private static IEnumerable<Type> GetConcreteTypes<T>(Assembly assembly) |
| 82 | + { |
| 83 | +#if NETFX_CORE || NETSTANDARD |
| 84 | + return assembly.DefinedTypes.Where( |
| 85 | +#else |
| 86 | + return assembly.GetTypes().Where( |
| 87 | +#endif |
| 88 | + t => |
| 89 | + { |
| 90 | + try |
| 91 | + { |
| 92 | +#if NETFX_CORE || NETSTANDARD |
| 93 | + return (t.IsSubclassOf(typeof(T)) || typeof(T).GetTypeInfo().IsAssignableFrom(t)) |
| 94 | + && !t.IsAbstract; |
| 95 | +#else |
| 96 | + return (t.IsSubclassOf(typeof(T)) || typeof(T).IsAssignableFrom(t)) && !t.IsAbstract; |
| 97 | +#endif |
| 98 | + } |
| 99 | + catch |
| 100 | + { |
| 101 | + return false; |
| 102 | + } |
| 103 | +#if NETFX_CORE || NETSTANDARD |
| 104 | + }).Select(t => t.AsType()); |
| 105 | +#else |
| 106 | + }); |
| 107 | +#endif |
| 108 | + } |
| 109 | + |
| 110 | + private static T GetDefaultOrSingleInstance<T>(IEnumerable<Type> types) where T : IDefaultable |
| 111 | + { |
| 112 | + var instances = types.Select( |
| 113 | + t => |
| 114 | + { |
| 115 | + try |
| 116 | + { |
| 117 | + return (T)Activator.CreateInstance(t); |
| 118 | + } |
| 119 | + catch |
| 120 | + { |
| 121 | + return default(T); |
| 122 | + } |
| 123 | + }).ToList(); |
| 124 | + |
| 125 | + return instances.Count > 1 ? instances.Single(instance => instance.IsDefault) : instances.SingleOrDefault(); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments