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