You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.8 KiB
58 lines
1.8 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using Autofac;
|
|
using PluginSystem;
|
|
|
|
namespace WpfApp4
|
|
{
|
|
|
|
public class PluginLoader
|
|
{
|
|
private readonly string _pluginDirectory;
|
|
|
|
public PluginLoader(string pluginDirectory)
|
|
{
|
|
_pluginDirectory = pluginDirectory;
|
|
}
|
|
|
|
// 加载插件并注册到Autofac容器
|
|
public void LoadPlugins(ContainerBuilder builder)
|
|
{
|
|
if (!Directory.Exists(_pluginDirectory))
|
|
{
|
|
Directory.CreateDirectory(_pluginDirectory);
|
|
return;
|
|
}
|
|
|
|
// 加载插件程序集
|
|
var pluginAssemblies = Directory.GetFiles(_pluginDirectory, "*.dll")
|
|
.Select(Assembly.LoadFrom)
|
|
.ToList();
|
|
|
|
// 注册所有实现IJobPlugin接口的类型
|
|
builder.RegisterAssemblyTypes(pluginAssemblies.ToArray())
|
|
.Where(t => typeof(IJobPlugin).IsAssignableFrom(t) && !t.IsAbstract)
|
|
//.As<IJobPlugin>()--错误加上就查不出来插件
|
|
.InstancePerDependency();
|
|
|
|
// 注册插件元数据
|
|
foreach (var assembly in pluginAssemblies)
|
|
{
|
|
var pluginTypes = assembly.GetTypes()
|
|
.Where(t => typeof(IJobPlugin).IsAssignableFrom(t) && !t.IsAbstract);
|
|
|
|
foreach (var type in pluginTypes)
|
|
{
|
|
// 可以存储插件元数据,如类型名称等
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|