diff --git a/NewLife.Core/Model/IPlugin.cs b/NewLife.Core/Model/IPlugin.cs index f8438e72d..0b4a2a9de 100644 --- a/NewLife.Core/Model/IPlugin.cs +++ b/NewLife.Core/Model/IPlugin.cs @@ -21,15 +21,13 @@ public interface IPlugin } /// 插件特性。用于判断某个插件实现类是否支持某个宿主 -[AttributeUsage(AttributeTargets.Class)] -public class PluginAttribute : Attribute +/// 实例化 +/// +[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] +public class PluginAttribute(String identity) : Attribute { /// 插件宿主标识 - public String Identity { get; set; } - - /// 实例化 - /// - public PluginAttribute(String identity) => Identity = identity; + public String Identity { get; set; } = identity; } /// 插件管理器 @@ -104,7 +102,9 @@ public void Load() Plugins = list.ToArray(); } - IEnumerable LoadPlugins() + /// 加载插件 + /// + public IEnumerable LoadPlugins() { // 此时是加载所有插件,无法识别哪些是需要的 foreach (var item in AssemblyX.FindAllPlugins(typeof(IPlugin), true)) diff --git a/NewLife.Core/Reflection/AssemblyX.cs b/NewLife.Core/Reflection/AssemblyX.cs index db6d5b056..7573e0907 100644 --- a/NewLife.Core/Reflection/AssemblyX.cs +++ b/NewLife.Core/Reflection/AssemblyX.cs @@ -366,39 +366,38 @@ public static IEnumerable FindAllPlugins(Type baseType, Boolean isLoadAsse } } } - if (isLoadAssembly) + if (!isLoadAssembly) yield break; + + foreach (var item in ReflectionOnlyGetAssemblies()) { - foreach (var item in ReflectionOnlyGetAssemblies()) - { - //// 如果excludeGlobalTypes为true,则指检查来自非GAC引用的程序集 - //if (excludeGlobalTypes && item.Asm.GlobalAssemblyCache) continue; + //// 如果excludeGlobalTypes为true,则指检查来自非GAC引用的程序集 + //if (excludeGlobalTypes && item.Asm.GlobalAssemblyCache) continue; - // 不搜索系统程序集,不搜索未引用基类所在程序集的程序集,优化性能 - if (item.IsSystemAssembly || !IsReferencedFrom(item.Asm, baseAssemblyName)) continue; + // 不搜索系统程序集,不搜索未引用基类所在程序集的程序集,优化性能 + if (item.IsSystemAssembly || !IsReferencedFrom(item.Asm, baseAssemblyName)) continue; - var ts = item.FindPlugins(baseType); - if (ts != null && ts.Count > 0) + var ts = item.FindPlugins(baseType); + if (ts != null && ts.Count > 0) + { + // 真实加载 + if (XTrace.Debug) { - // 真实加载 - if (XTrace.Debug) - { - // 如果是本目录的程序集,去掉目录前缀 - var file = item.Asm.Location; - var root = AppDomain.CurrentDomain.BaseDirectory; - if (!root.IsNullOrEmpty() && file.StartsWithIgnoreCase(root)) file = file.Substring(root.Length).TrimStart("\\"); - XTrace.WriteLine("AssemblyX.FindAllPlugins(\"{0}\") => {1}", baseType.FullName, file); - } - var asm2 = Assembly.LoadFrom(item.Asm.Location); - ts = Create(asm2)?.FindPlugins(baseType); - if (ts != null) + // 如果是本目录的程序集,去掉目录前缀 + var file = item.Asm.Location; + var root = AppDomain.CurrentDomain.BaseDirectory; + if (!root.IsNullOrEmpty() && file.StartsWithIgnoreCase(root)) file = file.Substring(root.Length).TrimStart("\\"); + XTrace.WriteLine("AssemblyX.FindAllPlugins(\"{0}\") => {1}", baseType.FullName, file); + } + var asm2 = Assembly.LoadFrom(item.Asm.Location); + ts = Create(asm2)?.FindPlugins(baseType); + if (ts != null) + { + foreach (var elm in ts) { - foreach (var elm in ts) + if (!list.Contains(elm)) { - if (!list.Contains(elm)) - { - list.Add(elm); - yield return elm; - } + list.Add(elm); + yield return elm; } } }