反射,插件接口

先创建下图的一个窗体,插件下没添加项目,是留给反射加载.dll插件的。在debug目录下加一个Plugin文件夹,将插件的.dll文件放在此文件夹下可以生效。
这里写图片描述

给方案添加一个单独的项目来写规范插件的接口,因为需要生成一个.dll给写插件的人调用
这里写图片描述
接口的代码如下:

using System.Windows.Forms;

namespace NotePad.PluginInterface
{
    public delegate TextBox GetTextBoxDelegate();
    public interface NotePadPluginInterface
    {
        //string Name { get; }

        //void Run(System.Windows.Forms.TextBox textBox);
        
        //这个接口可以只要表示插件名称Name属性,和功能的Run(Textbox textbox)的方法,
        //在主程序根据Name生成ToolStripMenuItem,并绑定事件,然后执行Run。
        //但是把ToolStripMenuItem给接口,插件可以实现更复杂的ToolStripMenuItem
        
        ToolStripMenuItem[] Items 
        {
            get;
            set;
        }

        TextBox textbox
        {
            get;
            set;
        }

        event GetTextBoxDelegate GetTextBoxEvent;//用来获取当前的textbox

        void Initize();//生成ToolStripMenuItem,并绑定事件等。

    }
}

窗体加载插件的代码

using NotePad.PluginInterface;
using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace NotePad
{
    public partial class NotePad : Form
    {
        public NotePad()
        {
            InitializeComponent();
        }
        private void NotePad_Load(object sender, EventArgs e)
        {
            string path = Assembly.GetExecutingAssembly().Location;
            string addtions = Path.Combine(Path.GetDirectoryName(path), "Plugin");
            string[] dlls = Directory.GetFiles(addtions, "*.dll");//获取插件.dll文件
            foreach (string dll in dlls)
            {
                Assembly assenmbly = Assembly.LoadFile(dll);//加载指定路径上的程序集文件的内容
                Type[] types = assenmbly.GetExportedTypes();//获取此程序集中定义的Public类型
                Type typeNotePadExtInterface = typeof(NotePadPluginInterface);
                foreach (Type type in types)
                {
                    //验证类型实现了NotePadExtInterface接口并且可以实例化
                    if (typeNotePadExtInterface.IsAssignableFrom(type) && !type.IsAbstract)
                    {
                        NotePadPluginInterface notePadEx = (NotePadPluginInterface)Activator.CreateInstance(type);//创建插件实例
                        notePadEx.Initize();//先初始化插件里的Item
                        this.PluginToolStripMenuItem.DropDownItems.AddRange(notePadEx.Items);//将插件里定义的Item加为窗体 "插件"项的子项
                        notePadEx.GetTextBoxEvent += NotePadEx_GetTextBoxEvent;
                    }
                }
            }
        }

        private TextBox NotePadEx_GetTextBoxEvent()
        {
            return textBox1; 
        }
    }
}

写插件
在插件的项目里引用规范插件的接口的程序集
这里写图片描述
插件的代码如下:

using NotePad.PluginInterface;
using System;
using System.Windows.Forms;

namespace NotePadPlugin
{
    public class NotePadPlugin: NotePadPluginInterface
    {
        public ToolStripMenuItem[] Items
        {
            get;
            set;
        }

        public TextBox textbox
        {
            get;
            set;
        }

        public event GetTextBoxDelegate GetTextBoxEvent;

        public void Initize()
        {
            ToolStripMenuItem switchCaseItem = new ToolStripMenuItem();
            switchCaseItem.Text = "切换大小写";

            ToolStripMenuItem ToUpperItem = new ToolStripMenuItem();
            ToUpperItem.Text = "大写";
            ToUpperItem.Click += ToUpperItem_Click;

            ToolStripMenuItem ToLowerItem = new ToolStripMenuItem();
            ToLowerItem.Text = "小写";
            ToLowerItem.Click += ToLowerItem_Click;

            switchCaseItem.DropDownItems.AddRange(new ToolStripMenuItem[] { ToUpperItem, ToLowerItem });

            ToolStripMenuItem clearTextItem = new ToolStripMenuItem();
            clearTextItem.Text = "清空";
            clearTextItem.Click += ClearText_Click;

            ToolStripMenuItem colorItem = new ToolStripMenuItem();
            colorItem.Text = "颜色";
            colorItem.Click += ColorItem_Click; ;

            Items = new ToolStripMenuItem[] { switchCaseItem, clearTextItem,colorItem };
        }

        private void ColorItem_Click(object sender, EventArgs e)
        {
            textbox = GetTextBoxEvent();
            using (ColorDialog colorDialog = new ColorDialog())
            {
                if (colorDialog.ShowDialog() == DialogResult.OK)
                {
                    textbox.ForeColor = colorDialog.Color;
                }
            }
        }

        private void ClearText_Click(object sender, EventArgs e)
        {
            textbox = GetTextBoxEvent();
            textbox.Text = "";
        }

        private void ToLowerItem_Click(object sender, EventArgs e)
        {
            textbox = GetTextBoxEvent();
            textbox.Text = textbox.Text.ToLower();
        }

        private void ToUpperItem_Click(object sender, EventArgs e)
        {
            textbox = GetTextBoxEvent();
            textbox.Text = textbox.Text.ToUpper();
        }
    }

}

将插件的项目生成的dll文件放在NotePad程序Plugin文件夹下之后,运行程序可以看到插件下多了子项,而且功能都实现了。
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值