2023-04-22 22:47:25 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace LawFirmContracts.DI
|
|
|
|
|
{
|
|
|
|
|
public class ServiceProviderLoader
|
|
|
|
|
{
|
2023-04-24 00:01:34 +04:00
|
|
|
|
/// <summary>
|
2023-04-22 22:47:25 +04:00
|
|
|
|
/// Загрузка всех классов-реализаций IImplementationExtension
|
2023-04-24 00:01:34 +04:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static List<IImplementationExtension?> GetImplementationExtensions()
|
2023-04-22 22:47:25 +04:00
|
|
|
|
{
|
2023-04-24 00:01:34 +04:00
|
|
|
|
// Список типов, по каждому из которых, должна быть выбрана наиболее приоритетная реализация
|
|
|
|
|
// IImplementationExtension должен быть последним, поскольку все классы реализации, наследуются от него и являются им в частности
|
|
|
|
|
Type[] types =
|
|
|
|
|
{
|
|
|
|
|
typeof(ILogicImplementationExtension),
|
|
|
|
|
typeof(IImplementationExtension)
|
|
|
|
|
};
|
|
|
|
|
var trueTypes = types.Select(x => (IImplementationExtension?)null).ToList();
|
2023-04-22 22:47:25 +04:00
|
|
|
|
var files = Directory.GetFiles(TryGetImplementationExtensionsFolder(), "*.dll", SearchOption.AllDirectories);
|
|
|
|
|
foreach (var file in files.Distinct())
|
|
|
|
|
{
|
|
|
|
|
Assembly asm = Assembly.LoadFrom(file);
|
|
|
|
|
foreach (var t in asm.GetExportedTypes())
|
|
|
|
|
{
|
2023-04-24 00:01:34 +04:00
|
|
|
|
for (var i = 0; i < types.Length; i++)
|
2023-04-22 22:47:25 +04:00
|
|
|
|
{
|
2023-04-24 00:01:34 +04:00
|
|
|
|
if (t.IsClass && types[i].IsAssignableFrom(t))
|
2023-04-22 22:47:25 +04:00
|
|
|
|
{
|
2023-04-24 00:01:34 +04:00
|
|
|
|
if (trueTypes[i] == null)
|
|
|
|
|
{
|
|
|
|
|
trueTypes[i] = (IImplementationExtension)Activator.CreateInstance(t)!;
|
|
|
|
|
}
|
|
|
|
|
else
|
2023-04-22 22:47:25 +04:00
|
|
|
|
{
|
2023-04-24 00:01:34 +04:00
|
|
|
|
var newSource = (IImplementationExtension)Activator.CreateInstance(t)!;
|
|
|
|
|
if (newSource.Priority > trueTypes[i].Priority)
|
|
|
|
|
{
|
|
|
|
|
trueTypes[i] = newSource;
|
|
|
|
|
}
|
2023-04-22 22:47:25 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-24 00:01:34 +04:00
|
|
|
|
return trueTypes;
|
2023-04-22 22:47:25 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string TryGetImplementationExtensionsFolder()
|
|
|
|
|
{
|
|
|
|
|
var directory = new DirectoryInfo(Directory.GetCurrentDirectory());
|
|
|
|
|
while (directory != null && !directory.GetDirectories("ImplementationExtensions", SearchOption.AllDirectories).Any(x => x.Name == "ImplementationExtensions"))
|
|
|
|
|
{
|
|
|
|
|
directory = directory.Parent;
|
|
|
|
|
}
|
|
|
|
|
return $"{directory?.FullName}\\ImplementationExtensions";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|