using Rhea.Common; using System.Collections.Generic; using System.Reflection; using Tiger.Model.Minsun; using Tiger.Model; using System.Linq; using Autofac; using System.IO; using System.Text.RegularExpressions; using System; using System.Threading; using System.Runtime.Loader; using Tiger.IBusiness; namespace Tiger.Api { public class PluginManager { public static List Plugins { get; set; } = new(); public static List Load(string path, params string[] patterns) { List assemblies = new(); foreach (var file in Directory.EnumerateFiles(path, "*.dll", SearchOption.AllDirectories).Where(filename => patterns.Any(pattern => Regex.IsMatch(filename, pattern)))) { var plugin = new Plugin(file); assemblies.Add(plugin.Assembly); Plugins.Add(plugin); } return assemblies; } }//endClass public class Plugin { public Plugin(string path) { Load(path); } #region Variables #endregion #region Propertys public string Id { get; set; } = Guid.NewGuid().ToString("N"); public string Name { get; set; } public string FullPath { get; set; } public bool IsActive { get; set; } public AssemblyLoadContext ALC { get; set; } public Assembly Assembly { get; set; } #endregion #region Functions /// /// 加载 /// private void Load(string fullPath) { FullPath = fullPath; //var file = new FileInfo(path); Name = Path.GetFileNameWithoutExtension(FullPath); //ALC = new AssemblyLoadContext(Name, true); //Assembly = ALC.LoadFromAssemblyPath(Path); //ALC.Unloading += context => { // IsActive = false; // Logger.Default.Info($"卸载{context.Name}程序集:" + string.Join(',', context.Assemblies.Select(x => x.FullName))); //}; ALC = AssemblyLoadContext.Default; Assembly = ALC.LoadFromAssemblyPath(FullPath); //Assembly.LoadFrom(path); IsActive = true; } /// /// 卸载 /// public void Unload() { ALC.Unload(); } /// /// 重新加载 /// public void Reload() { } #endregion }//endClass }