Path: redb-route/redb.Route/Components/ComponentConstruction.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/Components/ComponentConstruction.cs
----
using System.Reflection;
using redb.Route.Abstractions;
namespace redb.Route.Components;
///
/// The one rule by which a component type found by a scan is created, for the engine's
/// and the catalog command of the XML code generator
/// alike: a concrete with a public constructor callable without arguments (none at all, or only
/// optional ones, as HttpComponent(string scheme = "http")), called with the parameters' defaults. A component
/// left out is reported with its reason, one call each, so a scheme never drops out in silence (mcp did from the
/// catalog, until its constructor without arguments; http did from the engine's scan).
///
internal static class ComponentConstruction
{
/// The reason given for a component without a constructor callable without arguments.
internal const string NoArgumentFreeConstructor = "no constructor callable without arguments";
///
/// The components among ; gets each component type left out and
/// the reason. Abstract types, interfaces and types that are not components are passed over without a report.
///
public static IEnumerable Create(IEnumerable types, Action skipped)
{
foreach (var type in types.Where(t => !t.IsAbstract && !t.IsInterface && typeof(IComponent).IsAssignableFrom(t)))
{
ConstructorInfo? constructor;
try
{
constructor = type.GetConstructors().FirstOrDefault(c => c.GetParameters().All(p => p.IsOptional));
}
catch (Exception ex) when (ex is TypeLoadException or FileNotFoundException or FileLoadException)
{
// A parameter type of a constructor lives in an assembly this process does not carry.
skipped(type, $"constructor types did not load ({ex.Message})");
continue;
}
if (constructor is null)
{
skipped(type, NoArgumentFreeConstructor);
continue;
}
IComponent component;
try
{
component = (IComponent)constructor.Invoke([.. constructor.GetParameters().Select(p => p.DefaultValue)]);
}
catch (Exception ex) when (ex is TargetInvocationException or MissingMethodException)
{
skipped(type, $"constructor failed ({ex.InnerException?.Message ?? ex.Message})");
continue;
}
yield return component;
}
}
}