123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Collections;
- using System.IO;
- using System.Reflection;
- using System.Windows;
- using System.Diagnostics;
- using AGV_WPF.Commands;
- using System.Collections.ObjectModel;
- namespace AGV_WPF.Plugin
- {
- public class PluginLoader
- {
- public static ArrayList pluginList = new ArrayList();
- public static ObservableCollection<ModuleInfo> moduleInfoList = new ObservableCollection<ModuleInfo>();
- public static void LoadAllPlugins()
- {
- //string pluginPath = System.IO.Path.Combine(Application.StartupPath, "plugins");
- string startPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
- string pluginPath = System.IO.Path.Combine(startPath, "plugins");
- if (!Directory.Exists(pluginPath))
- {
- Directory.CreateDirectory(pluginPath);
- }
- string[] files = Directory.GetFiles(pluginPath);
- foreach (string file in files)
- {
- if (file.ToLower().EndsWith(".dll"))
- {
- try
- {
- Assembly ab = Assembly.LoadFrom(file);
- Type[] types = ab.GetTypes();
- foreach (Type t in types)
- {
- //Console.WriteLine(t.FullName);
- if (t.GetInterface("Iplugin") != null)
- {
- Iplugin plugin = (Iplugin)ab.CreateInstance(t.FullName);
- pluginList.Add(plugin);//创建插件实例
- ModuleInfo info = new ModuleInfo();
- info.ModulePath = file;
- info.ModuleName = plugin.PluginName;
- info.ModuleSetCommand.ExecuteAction = new Action<object>(delegate(object o)
- {
- plugin.Fun(o);
- });
- info.ModuleOpenCommand.ExecuteAction = new Action<object>(delegate(object o)
- {
- Process p = new Process();
- p.StartInfo.FileName = System.IO.Path.GetDirectoryName(file);
- p.Start();
- });
- moduleInfoList.Add(info);
- }
- }
- }
- catch (System.Exception ex)
- {
- string fileName = System.IO.Path.GetFileName(file);
- string str = string.Format("插件{0}加载错误", fileName);
- throw new Exception(str);
- }
- }
- }
- Console.WriteLine(string.Format("Plugin Count:{0}", pluginList.Count));
- }
- }
- public class ModuleInfo
- {
- public string ModuleName { get; set; }
- public string ModulePath { get; set; }
- public DelegateCommand ModuleSetCommand { get; set; }
- public DelegateCommand ModuleOpenCommand { get; set; }
- public ModuleInfo()
- {
- ModuleSetCommand = new DelegateCommand();
- //ModuleSetCommand.ExecuteAction = new Action<object>(ModuleSet);
- ModuleOpenCommand = new DelegateCommand();
- /*ModuleOpenCommand.ExecuteAction = new Action<object>(delegate(object o)
- {
- MessageBox.Show("hello");
- });*/
- }
- }
- }
|