| | | 1 | | using NexusLabs.Needlr.Injection.Reflection.PluginFactories; |
| | | 2 | | using NexusLabs.Needlr.Injection.Reflection.TypeFilterers; |
| | | 3 | | using NexusLabs.Needlr.Injection.Reflection.TypeRegistrars; |
| | | 4 | | |
| | | 5 | | namespace NexusLabs.Needlr.Injection.Reflection; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides built-in handlers for reflection fallback scenarios. |
| | | 9 | | /// Use these with <see cref="SyringeReflectionExtensions.WithReflectionFallbackHandler"/> to control |
| | | 10 | | /// what happens when source-generated components are not available. |
| | | 11 | | /// </summary> |
| | | 12 | | public static class ReflectionFallbackHandlers |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// A handler that throws an <see cref="InvalidOperationException"/> when reflection fallback occurs. |
| | | 16 | | /// Use this to enforce source-generation in AOT/trimmed applications. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <example> |
| | | 19 | | /// <code> |
| | | 20 | | /// var syringe = new Syringe() |
| | | 21 | | /// .WithReflectionFallbackHandler(ReflectionFallbackHandlers.ThrowException); |
| | | 22 | | /// </code> |
| | | 23 | | /// </example> |
| | | 24 | | public static void ThrowException(ReflectionFallbackContext context) |
| | | 25 | | { |
| | 4 | 26 | | ArgumentNullException.ThrowIfNull(context); |
| | 4 | 27 | | throw new InvalidOperationException( |
| | 4 | 28 | | $"Reflection fallback detected for {context.ComponentName}. " + |
| | 4 | 29 | | $"{context.Reason} " + |
| | 4 | 30 | | $"Expected: {context.GeneratedComponentType?.Name ?? "source-generated component"}, " + |
| | 4 | 31 | | $"Fallback: {context.ReflectionComponentType.Name}. " + |
| | 4 | 32 | | "Add [assembly: GenerateTypeRegistry(...)] to enable source generation, " + |
| | 4 | 33 | | "or call .UsingReflection() to explicitly opt into reflection-based discovery."); |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// A handler that writes a warning to <see cref="Console.Error"/> when reflection fallback occurs. |
| | | 38 | | /// Useful for development/debugging to identify missing source generation. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <example> |
| | | 41 | | /// <code> |
| | | 42 | | /// var syringe = new Syringe() |
| | | 43 | | /// .WithReflectionFallbackHandler(ReflectionFallbackHandlers.LogWarning); |
| | | 44 | | /// </code> |
| | | 45 | | /// </example> |
| | | 46 | | public static void LogWarning(ReflectionFallbackContext context) |
| | | 47 | | { |
| | 1 | 48 | | ArgumentNullException.ThrowIfNull(context); |
| | 1 | 49 | | Console.Error.WriteLine( |
| | 1 | 50 | | $"[Needlr Warning] Reflection fallback for {context.ComponentName}: {context.Reason}"); |
| | 1 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// A handler that does nothing when reflection fallback occurs. |
| | | 55 | | /// This is the default behavior - silent fallback to reflection. |
| | | 56 | | /// </summary> |
| | | 57 | | public static void Silent(ReflectionFallbackContext context) |
| | | 58 | | { |
| | | 59 | | // Intentionally empty - silent fallback |
| | 1 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Creates context for TypeRegistrar fallback. |
| | | 64 | | /// </summary> |
| | 6 | 65 | | public static ReflectionFallbackContext CreateTypeRegistrarContext() => new() |
| | 6 | 66 | | { |
| | 6 | 67 | | ComponentName = "TypeRegistrar", |
| | 6 | 68 | | Reason = "No source-generated TypeRegistry found via NeedlrSourceGenBootstrap.", |
| | 6 | 69 | | ReflectionComponentType = typeof(ReflectionTypeRegistrar), |
| | 6 | 70 | | GeneratedComponentType = null |
| | 6 | 71 | | }; |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Creates context for TypeFilterer fallback. |
| | | 75 | | /// </summary> |
| | 2 | 76 | | public static ReflectionFallbackContext CreateTypeFiltererContext() => new() |
| | 2 | 77 | | { |
| | 2 | 78 | | ComponentName = "TypeFilterer", |
| | 2 | 79 | | Reason = "No source-generated TypeRegistry found via NeedlrSourceGenBootstrap.", |
| | 2 | 80 | | ReflectionComponentType = typeof(ReflectionTypeFilterer), |
| | 2 | 81 | | GeneratedComponentType = null |
| | 2 | 82 | | }; |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Creates context for PluginFactory fallback. |
| | | 86 | | /// </summary> |
| | 1 | 87 | | public static ReflectionFallbackContext CreatePluginFactoryContext() => new() |
| | 1 | 88 | | { |
| | 1 | 89 | | ComponentName = "PluginFactory", |
| | 1 | 90 | | Reason = "No source-generated TypeRegistry found via NeedlrSourceGenBootstrap.", |
| | 1 | 91 | | ReflectionComponentType = typeof(ReflectionPluginFactory), |
| | 1 | 92 | | GeneratedComponentType = null |
| | 1 | 93 | | }; |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Creates context for AssemblyProvider fallback. |
| | | 97 | | /// </summary> |
| | 1 | 98 | | public static ReflectionFallbackContext CreateAssemblyProviderContext() => new() |
| | 1 | 99 | | { |
| | 1 | 100 | | ComponentName = "AssemblyProvider", |
| | 1 | 101 | | Reason = "No source-generated TypeRegistry found via NeedlrSourceGenBootstrap.", |
| | 1 | 102 | | ReflectionComponentType = typeof(AssemblyProviderBuilder), |
| | 1 | 103 | | GeneratedComponentType = null |
| | 1 | 104 | | }; |
| | | 105 | | } |