| | | 1 | | using AmbientServices.Utilities; |
| | | 2 | | using System; |
| | | 3 | | using System.Diagnostics; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Reflection; |
| | | 6 | | |
| | | 7 | | namespace AmbientServices.Extensions; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// A static class with extension functions for <see cref="System.Reflection.Assembly"/>. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// <pitch>Reflection over assemblies that don't fully load: enumerate whatever types <em>are</em> loadable without the |
| | | 14 | | /// <pledge><see cref="GetLoadableTypes"/> never propagates <see cref="ReflectionTypeLoadException"/>; when some types f |
| | | 15 | | /// </remarks> |
| | | 16 | | public static class AssemblyExtensions |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Enumerates loadable types from the assembly, even if referencing one or more of the types throws a <see cref="Re |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="assembly">The assembly to attempt to enumerate types from.</param> |
| | | 22 | | /// <returns>An enumeration of <see cref="Type"/>s.</returns> |
| | | 23 | | [DebuggerNonUserCode] |
| | | 24 | | public static Type[] GetLoadableTypes(this System.Reflection.Assembly assembly) |
| | | 25 | | { |
| | | 26 | | #if NET5_0_OR_GREATER |
| | | 27 | | ArgumentNullException.ThrowIfNull(assembly); |
| | | 28 | | #else |
| | | 29 | | if (assembly is null) throw new ArgumentNullException(nameof(assembly)); |
| | | 30 | | #endif |
| | | 31 | | Type[] types; |
| | | 32 | | //int loop = 0; |
| | | 33 | | //do |
| | | 34 | | //{ |
| | | 35 | | try |
| | | 36 | | { |
| | | 37 | | types = assembly.GetTypes(); |
| | | 38 | | } |
| | | 39 | | catch (ReflectionTypeLoadException ex) |
| | | 40 | | { |
| | | 41 | | // can't figure out how to force this exception for the moment, but this code is from several popular posts |
| | | 42 | | types = AssemblyUtilities.TypesFromException(ex); |
| | | 43 | | } |
| | | 44 | | // if ((types?.Length ?? 0) > 0) break; |
| | | 45 | | // System.Threading.Thread.Sleep(100); |
| | | 46 | | //} while (loop++ < 5); |
| | | 47 | | return types ?? Array.Empty<Type>(); |
| | | 48 | | } |
| | | 49 | | /// <summary> |
| | | 50 | | /// Checks to see if this assembly refers directly to the specified assembly. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <param name="assembly">The assembly to check.</param> |
| | | 53 | | /// <param name="referredToAssembly">The referred to assembly to look for.</param> |
| | | 54 | | /// <returns><b>true</b> if <paramref name="assembly"/> refers to <paramref name="referredToAssembly"/>.</returns> |
| | | 55 | | public static bool DoesAssemblyReferDirectlyToAssembly(this System.Reflection.Assembly assembly, Assembly referredTo |
| | | 56 | | { |
| | | 57 | | #if NET5_0_OR_GREATER |
| | 3 | 58 | | ArgumentNullException.ThrowIfNull(assembly); |
| | | 59 | | #else |
| | | 60 | | if (assembly is null) throw new ArgumentNullException(nameof(assembly)); |
| | | 61 | | #endif |
| | 3 | 62 | | return (assembly == referredToAssembly || assembly.GetReferencedAssemblies().FirstOrDefault(a => a.FullName == r |
| | | 63 | | } |
| | | 64 | | #if LATER |
| | | 65 | | /// <summary> |
| | | 66 | | /// Checks to see if this assembly refers to the specified assembly. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <param name="assembly">The assembly to check.</param> |
| | | 69 | | /// <param name="referredToAssembly">The referred to assembly to look for.</param> |
| | | 70 | | /// <returns><b>true</b> if <paramref name="assembly"/> refers to <paramref name="referredToAssembly"/>.</returns> |
| | | 71 | | public static bool DoesAssemblyReferToAssembly(this System.Reflection.Assembly assembly, Assembly referredToAssembly |
| | | 72 | | { |
| | | 73 | | // I attempted to implement a recursive algorithm, but it ended up causing hangs in the tests, even when I added |
| | | 74 | | // and I'm beginning to doubt whether a recursive algorithm is needed to list indirectly-referenced assemblies |
| | | 75 | | return DoesAssemblyReferDirectlyToAssembly(assembly, referredToAssembly); |
| | | 76 | | } |
| | | 77 | | #endif |
| | | 78 | | } |