Path: redb-route/redb.Route/Core/RouteBuilderDiscovery.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Core/RouteBuilderDiscovery.cs ---- using System.Reflection; namespace redb.Route.Core; /// /// Finds the types of a set of assemblies — the one place both /// and /// go through. /// /// A candidate is a public, non-abstract, closed class deriving from — a nested public type /// included, excluded. Which of them to take is the caller's filter, as Apache /// Camel's include/exclude patterns: there is no attribute, a type stays out by its filter or by being non-public /// or abstract. The order is by full name, for a reproducible log; it carries no meaning, since a builder's /// OnException, Intercept* and OnCompletion apply to its own routes only. /// /// /// Nothing is skipped silently: an assembly whose types cannot all be loaded is an error, not a partial scan — /// a route builder among the types that failed would otherwise vanish without a word. /// /// internal static class RouteBuilderDiscovery { /// The route builder types of that pass , by full name. /// Assemblies to scan; at least one. /// Which candidates to take; null takes them all. internal static IReadOnlyList FindBuilderTypes(IEnumerable assemblies, Func? filter) { ArgumentNullException.ThrowIfNull(assemblies); var distinct = assemblies.Distinct().ToList(); if (distinct.Count == 0) throw new ArgumentException("At least one assembly is required to discover route builders.", nameof(assemblies)); if (distinct.Any(a => a is null)) throw new ArgumentException("The assemblies to discover route builders in must not contain null.", nameof(assemblies)); return distinct .SelectMany(LoadTypes) .Where(IsCandidate) .Where(t => filter?.Invoke(t) ?? true) .OrderBy(t => t.FullName, StringComparer.Ordinal) .ToList(); } /// /// Creates a discovered builder through its public parameterless constructor — the context path, which has no /// container to inject from. /// internal static RouteBuilder Create(Type builderType) { if (builderType.GetConstructor(Type.EmptyTypes) is null) throw new InvalidOperationException( $"Route builder '{builderType.FullName}' has no public parameterless constructor, so it cannot be " + "created by assembly discovery on the context. Register it through dependency injection " + "(RedbRouteBuilder.AddRouteBuilders), add an instance with AddRoutes(builder), or leave it out with the filter."); try { return (RouteBuilder)Activator.CreateInstance(builderType)!; } catch (TargetInvocationException ex) { throw new InvalidOperationException( $"Route builder '{builderType.FullName}' threw from its constructor during assembly discovery.", ex.InnerException ?? ex); } } private static bool IsCandidate(Type type) => type.IsClass && !type.IsAbstract && !type.ContainsGenericParameters && type.IsVisible && type != typeof(InlineRouteBuilder) && typeof(RouteBuilder).IsAssignableFrom(type); private static Type[] LoadTypes(Assembly assembly) { try { return assembly.GetTypes(); } catch (ReflectionTypeLoadException ex) { var reasons = ex.LoaderExceptions .Where(e => e is not null) .Select(e => e!.Message) .Distinct() .ToList(); throw new InvalidOperationException( $"Not every type of assembly '{assembly.FullName}' could be loaded, so its route builders cannot be " + $"discovered reliably: {string.Join("; ", reasons)}", ex); } } }