对插件开发不太熟,这个例子是最基础的一个

记事本插件开发               记事本插件开发

下面直接贴后台代码

记事本Form1中

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using 记事本.IEditPlus;
namespace 记事本
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //浏览某个路径下的文件夹,寻找dll文件
            //找到当前程序所在的目录并且和这个程序对应的文件夹拼接起来
            string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "LibOne");
            //从这个路径中获取所有的dll文件
            string[] files = Directory.GetFiles(path, "*.dll");
            //遍历所有的文件
            for (int i = 0; i < files.Length; i++)
            {
                //获取这个路径下的所有的程序集
               Assembly ass=  Assembly.LoadFile(files[i]);
                Type Eidt= typeof(IEditp);//获取这个接口的Type
                //获取这个程序集中公共的类型
               Type[] tps = ass.GetExportedTypes();
                //遍历所有公共的类型
               for (int j = 0; j < tps.Length; j++)
               {
                   //tps[i]能不能赋值给接口对象,并且不能是抽象
                   if (Eidt.IsAssignableFrom(tps[j])&&!tps[j].IsAbstract)
                   {
                       //创建接口类型的实例,强转了
                      IEditp iedit= (IEditp)Activator.CreateInstance(tps[j]);
                       //把插件的名字获取到
                      string name= iedit.Name;
                       //把插件功能的名字添加到菜单中
                     ToolStripItem tsi=  tsm.DropDownItems.Add(name);//主程序的菜单上就有这个功能的名字了
                       //把接口存到该菜单的tag中
                     tsi.Tag = iedit;
                       //注册一个点击事件
                     tsi.Click += new EventHandler(tsi_Click);
                   }

               }

            }
        }

        void tsi_Click(object sender, EventArgs e)
        {
            //谁触发了这个方法
            ToolStripItem tsi=  sender as ToolStripItem;
            //从tag属性中获取接口
            IEditp edi= tsi.Tag as IEditp;
            //获取接口的方法,把文本框传进去,并且接收一下
           textBox1.Text= edi.ConvertString(textBox1);
        }
    }
}
Form1

相关文章:

  • 2021-12-17
  • 2021-07-09
  • 2021-12-01
  • 2021-08-09
  • 2022-02-03
  • 2021-12-27
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2023-04-10
  • 2021-06-08
  • 2021-04-11
  • 2021-12-18
  • 2021-08-31
相关资源
相似解决方案