Skip to content

Commit

Permalink
优化IPlugin,支持从Plugins插件目录加载插件
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed May 12, 2024
1 parent 0f36d35 commit ec89e24
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 35 deletions.
16 changes: 8 additions & 8 deletions NewLife.Core/Model/IPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ public interface IPlugin
}

/// <summary>插件特性。用于判断某个插件实现类是否支持某个宿主</summary>
[AttributeUsage(AttributeTargets.Class)]
public class PluginAttribute : Attribute
/// <remarks>实例化</remarks>
/// <param name="identity"></param>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class PluginAttribute(String identity) : Attribute
{
/// <summary>插件宿主标识</summary>
public String Identity { get; set; }

/// <summary>实例化</summary>
/// <param name="identity"></param>
public PluginAttribute(String identity) => Identity = identity;
public String Identity { get; set; } = identity;
}

/// <summary>插件管理器</summary>
Expand Down Expand Up @@ -104,7 +102,9 @@ public void Load()
Plugins = list.ToArray();
}

IEnumerable<Type> LoadPlugins()
/// <summary>加载插件</summary>
/// <returns></returns>
public IEnumerable<Type> LoadPlugins()
{
// 此时是加载所有插件,无法识别哪些是需要的
foreach (var item in AssemblyX.FindAllPlugins(typeof(IPlugin), true))
Expand Down
53 changes: 26 additions & 27 deletions NewLife.Core/Reflection/AssemblyX.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,39 +366,38 @@ public static IEnumerable<Type> 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;
}
}
}
Expand Down

0 comments on commit ec89e24

Please sign in to comment.