| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | #nullable enable |
| | 4 | |
|
| | 5 | | using System.Collections.Concurrent; |
| | 6 | | using System.Collections.Immutable; |
| | 7 | | using System.Globalization; |
| | 8 | | using System.Reflection; |
| | 9 | |
|
| | 10 | | namespace Ice.Internal; |
| | 11 | |
|
| | 12 | | /// <summary>Implements <see cref="SliceLoader" /> by searching for generated classes in the specified assemblies and |
| | 13 | | /// their referenced assemblies.</summary> |
| | 14 | | internal sealed class AssemblySliceLoader : SliceLoader |
| | 15 | | { |
| | 16 | | internal static AssemblySliceLoader Empty { get; } = |
| | 17 | | new AssemblySliceLoader(ImmutableDictionary<string, Type>.Empty); |
| | 18 | |
|
| | 19 | | private readonly IReadOnlyDictionary<string, Type> _dict; |
| | 20 | |
|
| | 21 | | /// <inheritdoc /> |
| | 22 | | public object? newInstance(string typeId) |
| | 23 | | { |
| | 24 | | if (_dict.TryGetValue(typeId, out Type? type)) |
| | 25 | | { |
| | 26 | | try |
| | 27 | | { |
| | 28 | | return Activator.CreateInstance(type); |
| | 29 | | } |
| | 30 | | catch (System.Exception exception) |
| | 31 | | { |
| | 32 | | throw new MarshalException( |
| | 33 | | $"Failed to create an instance of class '{type.FullName}' for type ID '{typeId}'.", exception); |
| | 34 | | } |
| | 35 | | } |
| | 36 | | else |
| | 37 | | { |
| | 38 | | return null; |
| | 39 | | } |
| | 40 | | } |
| | 41 | |
|
| | 42 | | /// <summary>Merge loaders into a single loader; duplicate entries are ignored.</summary> |
| | 43 | | internal static AssemblySliceLoader Merge(IEnumerable<AssemblySliceLoader> loaders) |
| | 44 | | { |
| | 45 | | if (loaders.Count() == 1) |
| | 46 | | { |
| | 47 | | return loaders.First(); |
| | 48 | | } |
| | 49 | | else |
| | 50 | | { |
| | 51 | | var dict = new Dictionary<string, Type>(); |
| | 52 | |
|
| | 53 | | foreach (AssemblySliceLoader loader in loaders) |
| | 54 | | { |
| | 55 | | foreach ((string typeId, Type factory) in loader._dict) |
| | 56 | | { |
| | 57 | | dict[typeId] = factory; |
| | 58 | | } |
| | 59 | | } |
| | 60 | | return dict.Count == 0 ? Empty : new AssemblySliceLoader(dict); |
| | 61 | | } |
| | 62 | | } |
| | 63 | |
|
| | 64 | | internal AssemblySliceLoader(IReadOnlyDictionary<string, Type> dict) => _dict = dict; |
| | 65 | | } |
| | 66 | |
|
| | 67 | | /// <summary>Creates Slice loaders from assemblies by mapping types in these assemblies.</summary> |
| | 68 | | internal sealed class AssemblySliceLoaderFactory |
| | 69 | | { |
| 1 | 70 | | internal static AssemblySliceLoaderFactory Instance { get; } = new AssemblySliceLoaderFactory(); |
| | 71 | |
|
| 1 | 72 | | private readonly ConcurrentDictionary<Assembly, AssemblySliceLoader> _cache = new(); |
| | 73 | |
|
| | 74 | | internal AssemblySliceLoader Get(Assembly assembly) |
| | 75 | | { |
| 1 | 76 | | if (_cache.TryGetValue(assembly, out AssemblySliceLoader? loader)) |
| | 77 | | { |
| 1 | 78 | | return loader; |
| | 79 | | } |
| 1 | 80 | | else if (assembly.GetCustomAttributes<SliceAttribute>().Any()) |
| | 81 | | { |
| 1 | 82 | | return _cache.GetOrAdd( |
| 1 | 83 | | assembly, |
| 1 | 84 | | assembly => |
| 1 | 85 | | { |
| 1 | 86 | | var dict = new Dictionary<string, Type>(); |
| 1 | 87 | |
|
| 1 | 88 | | foreach (Type type in assembly.GetTypes()) |
| 1 | 89 | | { |
| 1 | 90 | | // We're only interested in generated Slice classes and exceptions. |
| 1 | 91 | | if (type.IsClass && type.GetSliceTypeId() is string typeId) |
| 1 | 92 | | { |
| 1 | 93 | | dict.Add(typeId, type); |
| 1 | 94 | |
|
| 1 | 95 | | if (type.GetCompactSliceTypeId() is int compactTypeId) |
| 1 | 96 | | { |
| 1 | 97 | | dict.Add(compactTypeId.ToString(CultureInfo.InvariantCulture), type); |
| 1 | 98 | | } |
| 1 | 99 | | } |
| 1 | 100 | | } |
| 1 | 101 | |
|
| 1 | 102 | | // Merge with the loaders of the referenced assemblies (recursive call). |
| 1 | 103 | | return AssemblySliceLoader.Merge( |
| 1 | 104 | | assembly.GetReferencedAssemblies().Select( |
| 1 | 105 | | assemblyName => Get(AppDomain.CurrentDomain.Load(assemblyName))).Append( |
| 1 | 106 | | new AssemblySliceLoader(dict))); |
| 1 | 107 | | }); |
| | 108 | | } |
| | 109 | | else |
| | 110 | | { |
| | 111 | | // We don't cache an assembly with no Slice attribute, and don't load/process its referenced assemblies. |
| 1 | 112 | | return AssemblySliceLoader.Empty; |
| | 113 | | } |
| | 114 | | } |
| | 115 | |
|
| 1 | 116 | | private AssemblySliceLoaderFactory() |
| | 117 | | { |
| | 118 | | // ensures it's a singleton. |
| 1 | 119 | | } |
| | 120 | | } |