DepartmentProject/DepartmentPortal/Common/ToolsModule/ManagmentDependency/ServiceProviderLoader.cs

56 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml.XPath;
using ToolsModule.ManagmentExtension;
namespace ToolsModule.ManagmentDependency
{
/// <summary>
/// Загрузчик данных
/// </summary>
public static partial class ServiceProviderLoader
{
private static readonly string _pathToImplementationExt = "..\\..\\..\\..\\ImplementationExtensions\\";
/// <summary>
/// Загрузка всех классов-реализаций IImplementationExtension
/// </summary>
/// <returns></returns>
public static List<IDependencyRegistration> GetImplementationExtensions()
{
var list = new List<IDependencyRegistration>();
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;
}
var asm = Assembly.LoadFrom(file);
foreach (var t in asm.GetExportedTypes())
{
if (t.IsClass && typeof(IDependencyRegistration).IsAssignableFrom(t))
{
list.Add((IDependencyRegistration)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);
}
}