| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="GeneratedMetadataBootstrapInvoker.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Concurrent; |
| | | 9 | | using System.Reflection; |
| | | 10 | | |
| | | 11 | | namespace MyNet.Metadata; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Invokes assembly-generated <c>ObservableMetadataBootstrap.Ensure</c> on first metadata access (lazy configuration). |
| | | 15 | | /// </summary> |
| | | 16 | | internal static class GeneratedMetadataBootstrapInvoker |
| | | 17 | | { |
| | | 18 | | private const string BootstrapTypeFullName = "MyNet.Metadata.Generated.ObservableMetadataBootstrap"; |
| | | 19 | | |
| | 12 | 20 | | private static readonly ConcurrentDictionary<Assembly, Action<Type>?> Invokers = new(); |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Applies generated metadata configuration for <paramref name="type"/> when present in its assembly. |
| | | 24 | | /// </summary> |
| | | 25 | | public static void Ensure(Type type) |
| | | 26 | | { |
| | 12252 | 27 | | ArgumentNullException.ThrowIfNull(type); |
| | | 28 | | |
| | 12252 | 29 | | var invoker = Invokers.GetOrAdd(type.Assembly, CreateInvoker); |
| | 12252 | 30 | | invoker?.Invoke(type); |
| | 897 | 31 | | } |
| | | 32 | | |
| | | 33 | | private static Action<Type>? CreateInvoker(Assembly assembly) |
| | | 34 | | { |
| | 33 | 35 | | var bootstrapType = assembly.GetType(BootstrapTypeFullName, throwOnError: false); |
| | | 36 | | |
| | 33 | 37 | | var ensureMethod = bootstrapType?.GetMethod( |
| | 33 | 38 | | "Ensure", |
| | 33 | 39 | | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, |
| | 33 | 40 | | binder: null, |
| | 33 | 41 | | types: [typeof(Type)], |
| | 33 | 42 | | modifiers: null); |
| | | 43 | | |
| | 33 | 44 | | return ensureMethod is null ? null : (type => ensureMethod.Invoke(null, [type])); |
| | | 45 | | } |
| | | 46 | | } |
| | | 47 | | |