2022-03-18 22:38:52 +04:00
|
|
|
|
using ToolsModule.Extensions;
|
|
|
|
|
using ToolsModule.Interfaces;
|
2021-03-28 19:15:55 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Xml.XPath;
|
|
|
|
|
|
2022-03-18 22:38:52 +04:00
|
|
|
|
namespace ToolsModule.BusinessLogics
|
2021-03-28 19:15:55 +04:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Загрузчик данных
|
|
|
|
|
/// </summary>
|
2021-03-28 19:30:56 +04:00
|
|
|
|
public static partial class ServiceProviderLoader
|
2021-03-28 19:15:55 +04:00
|
|
|
|
{
|
|
|
|
|
private static readonly string _configFileName = "DepartmentPortal.config";
|
|
|
|
|
|
|
|
|
|
private static readonly string _pathToImplementationExt = "..\\..\\..\\..\\ImplementationExtensions\\";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение данных с файла настроек
|
|
|
|
|
/// </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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Загрузка всех классов-реализаций IImplementationExtension
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static List<IImplementationExtension> GetImplementationExtensions()
|
|
|
|
|
{
|
|
|
|
|
var list = new List<IImplementationExtension>();
|
|
|
|
|
if(Directory.Exists(_pathToImplementationExt))
|
|
|
|
|
{
|
|
|
|
|
var files = Directory.GetFiles(_pathToImplementationExt, "*.dll", SearchOption.AllDirectories);
|
|
|
|
|
var loadedFiles = new List<string>();
|
|
|
|
|
foreach(var file in files.Distinct())
|
|
|
|
|
{
|
|
|
|
|
if(loadedFiles.Contains(file.GetFileName()))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Assembly asm = Assembly.LoadFrom(file);
|
|
|
|
|
foreach (var t in asm.GetExportedTypes())
|
|
|
|
|
{
|
|
|
|
|
if (t.IsClass && typeof(IImplementationExtension).IsAssignableFrom(t))
|
|
|
|
|
{
|
|
|
|
|
list.Add((IImplementationExtension)Activator.CreateInstance(t));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
loadedFiles.Add(file.GetFileName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение имени файла
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="fileName"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private static string GetFile(string fileName) => Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), fileName);
|
|
|
|
|
}
|
|
|
|
|
}
|