Path: redb-route/redb.Route.Xml/Packaging/PackageTypeResolver.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/Packaging/PackageTypeResolver.cs
----
using System.Reflection;
using System.Runtime.Loader;
namespace redb.Route.Xml.Packaging;
///
/// The type lookup of the packaging checks with a build output at hand (redb-route-xml check|pack --bin): the
/// assemblies of that output first, then the shared framework the worker runs on. A worker resolves a markup type name
/// against everything its process has loaded, which includes framework assemblies a project's bin/ never carries
/// (System.Net.Http for ); a check that looked in bin/ only
/// refused markup the worker runs.
///
public static class PackageTypeResolver
{
///
/// Loads every assembly of into a load context of its own and returns the lookup.
/// A type name is Full.Name or Full.Name, Assembly.
///
/// The build output directory.
/// Receives a line for every DLL in the directory that is not a loadable assembly.
public static Func Create(string binDir, Action? warn = null)
{
ArgumentException.ThrowIfNullOrWhiteSpace(binDir);
var context = new AssemblyLoadContext("route-pack-check", isCollectible: false);
context.Resolving += (loadContext, assemblyName) =>
{
var candidate = Path.Combine(binDir, assemblyName.Name + ".dll");
return File.Exists(candidate) ? loadContext.LoadFromAssemblyPath(candidate) : null;
};
foreach (var dll in Directory.GetFiles(binDir, "*.dll"))
{
try
{
context.LoadFromAssemblyPath(dll);
}
catch (BadImageFormatException)
{
warn?.Invoke($"warning: {Path.GetFileName(dll)} is not a loadable assembly — skipped by the deep bean checks.");
}
}
return typeName =>
{
ArgumentException.ThrowIfNullOrWhiteSpace(typeName);
var comma = typeName.IndexOf(',');
var fullName = (comma >= 0 ? typeName[..comma] : typeName).Trim();
var assemblyName = comma >= 0 ? typeName[(comma + 1)..].Trim() : null;
var assemblies = assemblyName is null
? context.Assemblies
: context.Assemblies.Where(a => string.Equals(a.GetName().Name, assemblyName, StringComparison.OrdinalIgnoreCase));
foreach (var assembly in assemblies)
{
if (assembly.GetType(fullName) is { } type)
return type;
}
return Type.GetType(typeName, throwOnError: false) ?? FrameworkType(fullName, assemblyName);
};
}
///
/// A type of the shared framework this process runs on: in the named assembly, or else in the assembly named like
/// the type's namespace or a prefix of it (System.Net.Http.HttpRequestException → System.Net.Http), and
/// only then in any framework assembly. Framework assemblies resolve their forwarders, so a type that moved
/// (System.Net.Sockets.SocketException) is found through the assembly its name suggests.
///
internal static Type? FrameworkType(string fullName, string? assemblyName)
{
var platform = PlatformAssemblies();
if (assemblyName is not null)
return platform.TryGetValue(assemblyName, out var named) ? Load(named)?.GetType(fullName) : null;
for (var dot = fullName.LastIndexOf('.'); dot > 0; dot = fullName.LastIndexOf('.', dot - 1))
{
if (platform.TryGetValue(fullName[..dot], out var path) && Load(path)?.GetType(fullName) is { } type)
return type;
}
foreach (var path in platform.Values)
{
if (Load(path)?.GetType(fullName) is { } type)
return type;
}
return null;
}
private static Dictionary PlatformAssemblies() => Platform.Value;
// The shared framework only (the directory System.Private.CoreLib is loaded from), not the tool's own dependencies,
// which the trusted platform list also holds: the check must not accept a type that neither bin/ nor the framework has.
private static readonly Lazy> Platform = new(() =>
{
var frameworkDir = Path.GetDirectoryName(typeof(object).Assembly.Location) ?? string.Empty;
var list = AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES") as string ?? string.Empty;
var byName = new Dictionary(StringComparer.OrdinalIgnoreCase);
foreach (var path in list.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries))
{
if (string.Equals(Path.GetDirectoryName(path), frameworkDir, StringComparison.OrdinalIgnoreCase))
byName.TryAdd(Path.GetFileNameWithoutExtension(path), path);
}
return byName;
});
private static Assembly? Load(string path)
{
try
{
return AssemblyLoadContext.Default.LoadFromAssemblyName(AssemblyName.GetAssemblyName(path));
}
catch (Exception ex) when (ex is BadImageFormatException or FileLoadException or FileNotFoundException)
{
return null;
}
}
}