/// <summary> /// 加载插件 /// </summary> private void LoadAllPlugins() { string[] files = Directory.GetFiles(Application.StartupPath + @"\plugins\"); int i = 0; PluginInfoAttribute typeAttribute = new PluginInfoAttribute(); foreach (string file in files) //遍历指定路径下的文件名 { string ext = file.Substring(file.LastIndexOf(".")); //检索文件类型 if (ext != ".dll") //查找.DLL文件(筛选1) { continue; //如果文件类型非.DLL执行下一轮循环 } try { Assembly tmp = Assembly.LoadFile(file); //载入DLL,并实例化反射对象 Type[] types = tmp.GetTypes(); //获取模块里面的类 foreach (Type t in types) //遍历模块里面的类 { if (IsValidPlugin(t)) //如果包含接口"CSPluginKernel.IPlugin",说明插件适配与该主程序(筛选2) { plugins.Add(tmp.CreateInstance(t.FullName)); //将类实例化并添加到plugins动态数组里。 object[] attbs = t.GetCustomAttributes(typeAttribute.GetType(), false); //获取程序集“信息属性”,版本,作者等。 PluginInfoAttribute attribute = null; //定义信息属性对象,并指为空 foreach (object attb in attbs) //遍历信息属性集合 { PluginInfoAttribute temP = attb as PluginInfoAttribute; //强制类型转换 if (temP != null) { attribute = temP; //这里temP等价于(PluginInfoAttribute)attb, //另外在这时为对象初始化,attribute将不为null attribute.Index = i; //为Index属性赋值 i++; break; } } //如果读到“信息属性”,并实例化“信息属性”对象 if (attribute != null) { this.piProperties.Add(attribute);//将信息属性对象添加到piProperties动态数组 } else { throw new Exception("未定义插件属性");//抛出异常 } } } } catch (Exception err) { MessageBox.Show(err.Message); } } //遍历“信息属性”动态数组piProperties foreach (PluginInfoAttribute pia in piProperties) { ToolStripMenuItem ts = new ToolStripMenuItem(pia.Name + pia.Version); ts.Click += new EventHandler(RunPlugin); //为该菜单项注册单击事件 toolStripDropDownButton1.DropDownItems.Add(ts); pia.Tag = ts; //将增加的信息菜单项添加到pia的附加信息里 } //遍历“插件对象”动态数组 foreach (IPlugin pi in plugins) //插件与主程序进行连接,如果成功则调用插件的OnLoad方法 { if (pi.Connect((IfuncObject)this, (IvarObject)this)) { pi.OnLoad(); } else { MessageBox.Show("Can not connect plugin!"); } } }
private void LoadAllPlugins() { string[] files = Directory.GetFiles(Application.StartupPath + "\\plugins\\"); int i = 0; PluginInfoAttribute typeAttribute = new PluginInfoAttribute(); foreach (string file in files) { string ext = file.Substring(file.LastIndexOf(".")); if (ext != ".dll") { continue; } try { Assembly tmp = Assembly.LoadFile(file); Type[] types = tmp.GetTypes(); bool ok = false; foreach (Type t in types) { if (IsValidPlugin(t)) { plugins.Add(tmp.CreateInstance(t.FullName)); object[] attbs = t.GetCustomAttributes(typeAttribute.GetType(), false); PluginInfoAttribute attribute = null; foreach (object attb in attbs) { if (attb is PluginInfoAttribute) { attribute = (PluginInfoAttribute)attb; attribute.Index = i; i++; ok = true; break; } } if (attribute != null) { this.piProperties.Add(attribute); } else { throw new Exception("未定义插件属性"); } if (ok) { break; } } } } catch (Exception err) { MessageBox.Show(err.Message); } } foreach (PluginInfoAttribute pia in piProperties) { pia.Tag = AddContextMenu(pia.Name, 插件ToolStripMenuItem.DropDownItems, new EventHandler(MenuClicked)); } foreach (IPlugin pi in plugins) { if (pi.Connect((IApplicationObject)this) == ConnectionResult.Connection_Success) { pi.OnLoad(); } else { MessageBox.Show("Can not connect plugin!"); } } }
private void LoadAllPlugins() { string[] files = Directory.GetFiles(Application.StartupPath + "\\plugins\\"); int i = 0; PluginInfoAttribute typeAttribute = new PluginInfoAttribute(); foreach (string file in files) { string ext = file.Substring(file.LastIndexOf(".")); if (ext != ".dll") { continue; } try { Assembly tmp = Assembly.LoadFile(file); Type[] types = tmp.GetTypes(); bool ok = false; foreach (Type t in types) { if (IsValidPlugin(t)) { plugins.Add(tmp.CreateInstance(t.FullName)); object[] attbs = t.GetCustomAttributes(typeAttribute.GetType(), false); PluginInfoAttribute attribute = null; foreach (object attb in attbs) { if (attb is PluginInfoAttribute) { attribute = (PluginInfoAttribute)attb; attribute.Index = i; i++; ok = true; break; } } if (attribute != null) { this.piProperties.Add(attribute); } else { throw new Exception("未定义插件属性"); } if (ok) { break; } } } } catch (Exception err) { MessageBox.Show(err.Message); } } //随机产生颜色 eMetroTileColor[] colors = Enum.GetValues(typeof(eMetroTileColor)) as eMetroTileColor[]; Random random = new Random(); foreach (PluginInfoAttribute pia in piProperties) { //MenuItem tmp = menuItem6.MenuItems.Add(pia.Name + " " + pia.Version + " [ " + pia.Author + " ]"); MetroTileItem MTI = new MetroTileItem(); //随机赋值一个颜色 MTI.TileColor = colors[random.Next(0, colors.Length)]; // eMetroTileColor.DarkGreen; MTI.Name = pia.Name; MTI.TitleText = pia.Name; // +pia.Author + pia.Version; //MTI.Text = pia.Author;// MTI.Symbol = "☺"; MTI.TitleTextAlignment = System.Drawing.ContentAlignment.MiddleCenter; MTI.Click += new EventHandler(RunPlugin); pia.Tag = MTI; this.itemContainer1.SubItems.Add(MTI); } foreach (IPlugin pi in plugins) { if (pi.Connect((IApplicationObject)this) == ConnectionResult.Connection_Success) { pi.OnLoad(); } else { MessageBox.Show("Can not connect plugin!"); } } }
public int LoadPlugs() { Plugs.Clear(); PlugsInfo.Clear(); if (!Directory.Exists(Environment.CurrentDirectory + @"\Plugins")) { return(0); } string[] files = Directory.GetFiles(Environment.CurrentDirectory + @"\Plugins"); PluginInfoAttribute typeAttribute = new PluginInfoAttribute(); foreach (var item in files) { string ext = Path.GetExtension(item); if (ext != ".dll") { continue; } try { Assembly tmp = Assembly.UnsafeLoadFrom(item); Type[] types = tmp.GetTypes(); // 遍历实例获得实现接口的类 foreach (Type t in types) { if (t.GetInterface(interType.Name) != null) { T plug = (T)tmp.CreateInstance(t.FullName); Plugs.Add(plug); object[] attbs = t.GetCustomAttributes(typeAttribute.GetType(), false); PluginInfoAttribute attribute = null; foreach (object attb in attbs) { if (attb is PluginInfoAttribute) { attribute = (PluginInfoAttribute)attb; break; } } if (attribute != null) { PlugsInfo.Add(attribute); } CallBack(); } } } catch (Exception exp) { throw exp; } } return(Plugs.Count); }