2022-03-19 23:06:06 +04:00
|
|
|
|
using System;
|
2021-03-28 19:15:55 +04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Xml.XPath;
|
2022-03-20 10:10:44 +04:00
|
|
|
|
using ToolsModule.ManagmentExtension;
|
2021-03-28 19:15:55 +04:00
|
|
|
|
|
2022-03-20 10:10:44 +04:00
|
|
|
|
namespace ToolsModule.ManagmentDependency
|
2021-03-28 19:15:55 +04:00
|
|
|
|
{
|
2022-03-19 23:06:06 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Загрузчик данных
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static partial class ServiceProviderLoader
|
2021-03-28 19:15:55 +04:00
|
|
|
|
{
|
2022-03-26 21:51:15 +04:00
|
|
|
|
private static readonly string _configFileName = "DepartmentPortal.config";
|
2021-03-28 19:15:55 +04:00
|
|
|
|
|
2022-03-26 21:51:15 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение данных с файла настроек (для releaze версии сборки)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="key"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string GetConfigData(string key)
|
|
|
|
|
{
|
|
|
|
|
var fileName = GetFile(_configFileName);
|
|
|
|
|
if (!File.Exists(fileName))
|
|
|
|
|
{
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var doc = new XPathDocument(fileName);
|
|
|
|
|
var nav = doc.CreateNavigator();
|
|
|
|
|
var data = nav.SelectDescendants(key, "", false);
|
|
|
|
|
data.MoveNext();
|
|
|
|
|
return data.Current.Value;
|
|
|
|
|
}
|
2021-03-28 19:15:55 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Загрузка всех классов-реализаций IImplementationExtension
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2022-03-19 22:54:25 +04:00
|
|
|
|
public static List<IDependencyRegistration> GetImplementationExtensions()
|
2021-03-28 19:15:55 +04:00
|
|
|
|
{
|
2022-03-19 22:54:25 +04:00
|
|
|
|
var list = new List<IDependencyRegistration>();
|
2022-03-26 21:51:15 +04:00
|
|
|
|
var files = Directory.GetFiles(TryGetImplementationExtensionsFolder(), "*.dll", SearchOption.AllDirectories);
|
|
|
|
|
var loadedFiles = new List<string>();
|
|
|
|
|
foreach (var file in files.Distinct())
|
2021-03-28 19:15:55 +04:00
|
|
|
|
{
|
2022-03-26 21:51:15 +04:00
|
|
|
|
if (loadedFiles.Contains(file.GetFileName()))
|
2021-03-28 19:15:55 +04:00
|
|
|
|
{
|
2022-03-26 21:51:15 +04:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
var asm = Assembly.LoadFrom(file);
|
|
|
|
|
foreach (var t in asm.GetExportedTypes())
|
|
|
|
|
{
|
|
|
|
|
if (t.IsClass && typeof(IDependencyRegistration).IsAssignableFrom(t))
|
2021-03-28 19:15:55 +04:00
|
|
|
|
{
|
2022-03-26 21:51:15 +04:00
|
|
|
|
list.Add((IDependencyRegistration)Activator.CreateInstance(t));
|
2021-03-28 19:15:55 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-26 21:51:15 +04:00
|
|
|
|
loadedFiles.Add(file.GetFileName());
|
2021-03-28 19:15:55 +04:00
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение имени файла
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="fileName"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private static string GetFile(string fileName) => Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), fileName);
|
2022-03-26 21:51:15 +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";
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-28 19:15:55 +04:00
|
|
|
|
}
|
|
|
|
|
}
|