Path: redb-route/redb.Route.Xml.CodeGen/ContributionLoader.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.Xml.CodeGen/ContributionLoader.cs
----
using System.Reflection;
using System.Runtime.Loader;
using redb.Route.Xml.Packaging;
namespace redb.Route.Xml.CodeGen;
///
/// Reads what the connector assemblies of a --bin directory bring to the tool: their XML element contributions
/// and their package checks ().
///
internal static class ContributionLoader
{
///
/// Loads every redb.Route.*.dll of into its own load context and creates its
/// contributions. An assembly the tool itself runs on ( returns it: redb.Route.Xml above
/// all) is never loaded from the directory: a second copy would give the connectors a different
/// type, the cast would fail and the check would report every connector
/// element as unknown. The tool's own assemblies are not scanned either, their elements are built in.
///
public static List Load(string binDir, Func shared)
=> LoadAll(binDir, shared).Contributions;
///
/// The contributions and the package checks of , read in one load context, as
/// reads the contributions.
///
public static (List Contributions, List Checks) LoadAll(
string binDir, Func shared)
// The file order is the file system's: alphabetical on NTFS, not on APFS or ext4. Nothing below may depend
// on which file comes first.
=> LoadAll(binDir, Directory.GetFiles(binDir, "redb.Route.*.dll"), shared);
///
/// Loads the given assemblies of in the given order. The tests call it with both orders
/// of the same files, because a directory listing gives whichever order its file system keeps.
///
public static List Load(
string binDir, IReadOnlyList dlls, Func shared)
=> LoadAll(binDir, dlls, shared).Contributions;
/// The contributions and the package checks of the given assemblies, in the given order.
public static (List Contributions, List Checks) LoadAll(
string binDir, IReadOnlyList dlls, Func shared)
{
var contributions = new List();
var checks = new List();
var seen = new HashSet(StringComparer.Ordinal);
var context = new AssemblyLoadContext($"contrib:{binDir}", isCollectible: false);
context.Resolving += (loadContext, assemblyName) =>
{
if (shared(assemblyName.Name) is { } own) return own;
var candidate = Path.Combine(binDir, assemblyName.Name + ".dll");
return File.Exists(candidate) ? loadContext.LoadFromAssemblyPath(candidate) : null;
};
foreach (var dll in dlls)
{
Assembly assembly;
try
{
var simpleName = AssemblyName.GetAssemblyName(dll).Name;
if (shared(simpleName) is not null)
continue;
// The Resolving hook may have already pulled this simple name while satisfying a dependency; loading
// the local copy on top would throw «assembly with same name is already loaded». First copy wins.
assembly = context.Assemblies.FirstOrDefault(a => a.GetName().Name == simpleName)
?? context.LoadFromAssemblyPath(dll);
}
catch (BadImageFormatException)
{
continue;
}
Type[] types;
try
{
types = assembly.GetTypes();
}
catch (ReflectionTypeLoadException ex)
{
types = [.. ex.Types.Where(t => t is not null)!];
}
foreach (var type in types)
{
if (type.IsAbstract || type.IsInterface || type.GetConstructor(Type.EmptyTypes) is null)
continue;
var contribution = typeof(IXmlElementContribution).IsAssignableFrom(type);
var check = type.IsPublic && typeof(IPackageCheck).IsAssignableFrom(type);
if ((!contribution && !check) || !seen.Add(type.FullName ?? type.Name))
continue;
var instance = Activator.CreateInstance(type)!;
if (contribution)
contributions.Add((IXmlElementContribution)instance);
if (check)
checks.Add((IPackageCheck)instance);
}
}
return (contributions, checks);
}
}