using System.Collections.Immutable; using System.Reflection; using Connected.Annotations; using Connected.Collections; namespace Connected.Instance; public static class Assemblies { private static readonly List _all; static Assemblies() { _all = new(); foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { if (assembly.GetCustomAttribute() is not null) _all.Add(assembly); } } internal static ImmutableList All => _all.ToImmutableList(); public static Dictionary QueryInterfaces(this Assembly assembly) where TAttribute : Attribute { var result = new Dictionary(); foreach (var type in assembly.GetTypes()) { if (type.IsAbstract || !type.IsClass) continue; var interfaces = type.GetInterfaces(); foreach (var i in interfaces) { if (i.GetCustomAttribute() is not null) result.Add(i, type); } } return result; } public static Dictionary QueryImplementations(this Assembly assembly) { var result = new Dictionary(); foreach (var type in assembly.GetTypes()) { if (type.IsAbstract || !type.IsClass) continue; if (type.GetInterface(typeof(T).FullName) is null) continue; var interfaces = type.GetInterfaces(); foreach (var i in interfaces) { if (i.GetInterface(typeof(T).FullName) is not null) result.Add(i, type); } } return result; } public static ImmutableList QueryImplementations() { var target = typeof(T).FullName; var result = new List(); foreach (var microService in _all) { var types = microService.GetTypes(); foreach (var type in types) { if (type.IsAbstract || type.IsPrimitive || type.IsInterface) continue; if (type.GetInterface(target) is not null) result.Add(type); } } result.SortByPriority(); return result.ToImmutableList(); } }