| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using System.Collections; |
| | 4 | | using System.Reflection; |
| | 5 | | using System.Runtime.InteropServices; |
| | 6 | |
|
| | 7 | | namespace Ice.Internal; |
| | 8 | |
|
| | 9 | | public static class AssemblyUtil |
| | 10 | | { |
| 1 | 11 | | public static readonly bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); |
| 1 | 12 | | public static readonly bool isMacOS = RuntimeInformation.IsOSPlatform(OSPlatform.OSX); |
| 1 | 13 | | public static readonly bool isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); |
| | 14 | |
|
| | 15 | | public static object createInstance(Type t) |
| | 16 | | { |
| | 17 | | try |
| | 18 | | { |
| 1 | 19 | | return System.Activator.CreateInstance(t); |
| | 20 | | } |
| 0 | 21 | | catch (MemberAccessException) |
| | 22 | | { |
| 0 | 23 | | return null; |
| | 24 | | } |
| 1 | 25 | | } |
| | 26 | |
|
| | 27 | | public static void preloadAssemblies() |
| | 28 | | { |
| 1 | 29 | | lock (_mutex) |
| | 30 | | { |
| 1 | 31 | | loadAssemblies(); // Lazy initialization |
| 1 | 32 | | } |
| 1 | 33 | | } |
| | 34 | |
|
| | 35 | | // |
| | 36 | | // Make sure that all assemblies that are referenced by this process |
| | 37 | | // are actually loaded. This is necessary so we can use reflection |
| | 38 | | // on any type in any assembly because the type we are after will |
| | 39 | | // most likely not be in the current assembly and, worse, may be |
| | 40 | | // in an assembly that has not been loaded yet. (Type.GetType() |
| | 41 | | // is no good because it looks only in the calling object's assembly |
| | 42 | | // and mscorlib.dll.) |
| | 43 | | // |
| | 44 | | private static void loadAssemblies() |
| | 45 | | { |
| 1 | 46 | | Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); |
| 1 | 47 | | List<Assembly> newAssemblies = null; |
| 1 | 48 | | foreach (Assembly a in assemblies) |
| | 49 | | { |
| 1 | 50 | | if (!_loadedAssemblies.Contains(a.FullName)) |
| | 51 | | { |
| 1 | 52 | | newAssemblies ??= new List<Assembly>(); |
| 1 | 53 | | newAssemblies.Add(a); |
| 1 | 54 | | _loadedAssemblies[a.FullName] = a; |
| | 55 | | } |
| | 56 | | } |
| 1 | 57 | | if (newAssemblies != null) |
| | 58 | | { |
| 1 | 59 | | foreach (Assembly a in newAssemblies) |
| | 60 | | { |
| 1 | 61 | | loadReferencedAssemblies(a); |
| | 62 | | } |
| | 63 | | } |
| 1 | 64 | | } |
| | 65 | |
|
| | 66 | | private static void loadReferencedAssemblies(Assembly a) |
| | 67 | | { |
| | 68 | | try |
| | 69 | | { |
| 1 | 70 | | AssemblyName[] names = a.GetReferencedAssemblies(); |
| 1 | 71 | | foreach (AssemblyName name in names) |
| | 72 | | { |
| 1 | 73 | | if (!_loadedAssemblies.ContainsKey(name.FullName)) |
| | 74 | | { |
| | 75 | | try |
| | 76 | | { |
| 1 | 77 | | var ra = Assembly.Load(name); |
| | 78 | | // |
| | 79 | | // The value of name.FullName may not match that of ra.FullName, so |
| | 80 | | // we record the assembly using both keys. |
| | 81 | | // |
| 1 | 82 | | _loadedAssemblies[name.FullName] = ra; |
| 1 | 83 | | _loadedAssemblies[ra.FullName] = ra; |
| 1 | 84 | | loadReferencedAssemblies(ra); |
| 1 | 85 | | } |
| 0 | 86 | | catch (System.Exception) |
| | 87 | | { |
| | 88 | | // Ignore assemblies that cannot be loaded. |
| 0 | 89 | | } |
| | 90 | | } |
| | 91 | | } |
| 1 | 92 | | } |
| 0 | 93 | | catch (PlatformNotSupportedException) |
| | 94 | | { |
| | 95 | | // Some platforms like UWP do not support using GetReferencedAssemblies |
| 0 | 96 | | } |
| 1 | 97 | | } |
| | 98 | |
|
| 1 | 99 | | private static readonly Hashtable _loadedAssemblies = []; // <string, Assembly> pairs. |
| 1 | 100 | | private static readonly object _mutex = new(); |
| | 101 | | } |