1、在程序运行的同时将插件文件复制到指定的插件目录下,插件自动被加载。不废话了直接上代码吧。需要的可以帮我贡献点积分,谢谢各位大佬了。
示例文件下载https://download.csdn.net/download/xingchengaiwei/896388912、主要功能代码如下。注意初学者建议 查看示例文件下载。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AbstractionLayer;
using Path = System.IO.Path;
namespace WPFIPluginDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//开始监控指定文件
StartMonitoring(GetCurrentProgramPath() + "\\Plugins");
//加载已存在的插件
LoadPlugins(GetCurrentProgramPath() + "\\Plugins");
}
/// <summary>
/// 文件监视
/// </summary>
private FileSystemWatcher _fileWatcher;
/// <summary>
/// 开始监视文件夹
/// </summary>
/// <param name="directoryPath"></param>
private void StartMonitoring(string directoryPath)
{
// 创建 FileSystemWatcher 实例
_fileWatcher = new FileSystemWatcher();
// 设置要监视的目录路径
_fileWatcher.Path = directoryPath;
// 设置要监视的更改类型(例如:修改、创建、删除)
_fileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
// 添加事件处理程序
_fileWatcher.Changed += OnChanged;
_fileWatcher.Created += OnChanged;
_fileWatcher.Deleted += OnChanged;
// 开启事件监听
_fileWatcher.EnableRaisingEvents = true;
Debug.WriteLine($"已开始监视文件夹: {directoryPath}");
}
/// <summary>
/// 当文件夹中内容发生改变时触发时间
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
private void OnChanged(object source, FileSystemEventArgs e)
{
//Debug.WriteLine($"文件或文件夹发生变更: {e.FullPath},事件类型: {e.ChangeType}");
if ((e.ChangeType & WatcherChangeTypes.Created) != 0)
{
if (File.Exists(e.FullPath))
{
Debug.WriteLine("创建了文件");
LoadPlugins(GetCurrentProgramPath() + "\\Plugins");
Debug.WriteLine($"文件或文件夹发生变更: {e.FullPath},事件类型: {e.ChangeType}");
}
else
{
Debug.WriteLine("创建了文件夹");
}
}
}
private bool IsDirectory(string path)
{
try
{
FileAttributes attr = File.GetAttributes(path);
if ((attr & FileAttributes.Directory) ==FileAttributes.Directory)
{
return true;
}
else
{
return false;
}
}
catch (Exception e)
{
return false;
}
}
/// <summary>
/// 停止监视文件夹
/// </summary>
public void StopMonitoring()
{
if (_fileWatcher != null)
{
// 停止引发事件
_fileWatcher.EnableRaisingEvents = false;
// 清理资源
_fileWatcher.Dispose();
_fileWatcher = null;
Debug.WriteLine("已停止监视文件夹");
}
}
/// <summary>
/// 点击加载插件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnSearchPlugins_OnClick(object sender, RoutedEventArgs e)
{
LoadPlugins(GetCurrentProgramPath()+"\\Plugins");
}
/// <summary>
/// 获取当前程序集的路径
/// </summary>
/// <returns></returns>
public static string GetCurrentProgramPath()
{
// 获取当前执行的程序集
var assembly = Assembly.GetEntryAssembly();
// 获取程序集所在的路径
var assemblyLocation = assembly.Location;
// 获取程序集所在的目录
var programPath = Path.GetDirectoryName(assemblyLocation);
return programPath;
}
/// <summary>
/// 插件列表
/// </summary>
public List<IPlugin> ListIPlugins = new List<IPlugin>();
/// <summary>
/// 加载指定插件
/// </summary>
/// <param name="pluginDirectory"></param>
public void LoadPlugins(string pluginDirectory)
{
try
{
Dispatcher.BeginInvoke(new Action(delegate
{
// 获取所有插件DLL
var pluginDlls = Directory.GetFiles(pluginDirectory, "*.exe", SearchOption.TopDirectoryOnly);
foreach (var dll in pluginDlls)
{
try
{
Assembly pluginAssembly = Assembly.LoadFrom(dll);
// 查找实现了IPlugin接口的类型
var pluginTypes = pluginAssembly.GetTypes().Where(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsInterface);
foreach (var pluginType in pluginTypes)
{
IPlugin plugin = (IPlugin)Activator.CreateInstance(pluginType);
plugin.Initialize();
// 管理插件的代码
ListIPlugins.Add(plugin);
}
}
catch (Exception e)
{
Debug.WriteLine(e);
//throw;
}
}
}));
}
catch (Exception e)
{
Debug.WriteLine(e);
//throw;
}
}
/// <summary>
/// 调用插件中的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnShowPlugin_OnClick(object sender, RoutedEventArgs e)
{
Type plugin = ListIPlugins[0].GetType();
MethodInfo showInfo = plugin.GetMethod("Show");//得到dll文件的中的方法
showInfo.Invoke(ListIPlugins[0], null);//方法调用
}
}
}