记事本、计算机插件案例

 一、通过反射技术做个记事本小插件

1.插件的思路

 

 

 

2.代码

2.1新建一个窗体

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 System.Reflection;
using 记事本.EditPlus;
namespace 记事本
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //窗体加载
            //窗体加载的时候直接去Lib目录中搜索dll文件
            //第一步 获取当前程序运行的目录

            //Lib路径
            string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Lib");
            //获取所有的lib文件夹中的dll文件
          string []files=  Directory.GetFiles(path, "*.dll");
            //遍历所有的dll文件路径
          foreach (string item in files)
          {
              //加载每个路径的程序集
              Assembly ass= Assembly.LoadFile(item);
              Type[]tps= ass.GetExportedTypes();//获取所有的公共的Type
              Type tpIEdit=typeof(IEditPlus);//获取接口的Type
              //遍历所有的Type对象
              for (int i = 0; i < tps.Length; i++)
              {
                  //判断每个ty对象是不是实现了接口
                  if (tpIEdit.IsAssignableFrom(tps[i])&&!tps[i].IsAbstract)
                  {
                      //创建实例
                      IEditPlus iedit = Activator.CreateInstance(tps[i]) as IEditPlus;
                      ////插件的名字显示到视图菜单中
                     ToolStripItem tsi= tsm.DropDownItems.Add(iedit.Name);
                      //
                     tsi.Tag = iedit;//把该接口对象存储到tsi对象的tag中
                      //并为该菜单注册一个单击事件
                     tsi.Click += new EventHandler(tsi_Click);

                  }
              }
          }
        }

        void tsi_Click(object sender, EventArgs e)
        {
            ToolStripItem tsi= sender as ToolStripItem;
            IEditPlus iedit= tsi.Tag as IEditPlus;
           textBox1.Text= iedit.ChangeString(textBox1);
        }
    }
}
Form1

 

 2.2新建一个类库【记事本.EditPlus】

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 记事本.EditPlus
{
   public interface IEditPlus
    {
       /// <summary>
       /// 插件的名字
       /// </summary>
        string Name { get; }
       /// <summary>
       /// 记事本的文本框
       /// </summary>
       /// <param name="tb">文本框</param>
       /// <returns></returns>
        string ChangeString(TextBox tb);
    }
}
interface IEditPlus

2.3新建一个类库【记事本.小写转大写】

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using 记事本.EditPlus;
namespace 记事本.小写转大写
{
   public class MyClass:IEditPlus
    {
        public string ChangeString(System.Windows.Forms.TextBox tb)
        {
           return tb.Text.ToUpper();
        }
       //插件的名字
        public string Name
        {
            get { return "小写转大写"; }
        }
    }
}
class MyClass:IEditPlus

 

3.插件最终效果图

 

 

 

 

二、通过反射技术制作关机小插件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProIShut;
using System.Diagnostics;
namespace ProShutDown
{
    public class ShutDownClass : IPlugin
    {
        public void GuanJi(int t)
        {
            //关机
            ShutDown(t.ToString());
            //返回一个k类型的默认值
            //defalut(K)
        }

       // GuanJi2<int,string>

      
        

        public void ShutDown(string second)
        {
            Process process = new Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.CreateNoWindow = true;
            process.Start();
            process.StandardInput.WriteLine("shutdown -s -f -t " + second);
            //process.StandardInput.WriteLine("dir c:");
            process.StandardInput.WriteLine("exit");

            process.Close();
            process.Dispose();
        }

        public string Name
        {
            get { return "关机"; }
        }
    }
}
ProShutDown类库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProIShut
{
    public interface IPlugin
    {
        string Name { get; }

        //负责关闭计算器
        void GuanJi(int t);
    }
}
ProIShut类库
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
using ProIShut;
namespace 使用反射制作关机插件
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Type t = null;
        object o;
        private void Form1_Load(object sender, EventArgs e)
        {
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "plug");
            string[] files = Directory.GetFiles(path);
            foreach (string item in files)
            {
                Assembly ass = Assembly.LoadFile(item);

                Type[] types = ass.GetExportedTypes();

                for (int i = 0; i < types.Length; i++)
                {
                    if (typeof(IPlugin).IsAssignableFrom(types[i]) && !types[i].IsAbstract)
                    {
                        //创建对象
                        o = Activator.CreateInstance(types[i]);

                        PropertyInfo pif = types[i].GetProperty("Name");
                        //t = types[i];

                        //关机
                        object o2 = pif.GetValue(o);

                        ToolStripItem tsi = 插件ToolStripMenuItem.DropDownItems.Add(o2.ToString());
                        //给添加的tsi选项注册单击事件
                        tsi.Tag = types[i];
                        tsi.Click += tsi_Click;

                    }
                }

            }

        }

        void tsi_Click(object sender, EventArgs e)
        {
            ToolStripItem ts = sender as ToolStripItem;
            Type t = ts.Tag as Type;

            MethodInfo mi = t.GetMethod("GuanJi");
            int n=3000;
            mi.Invoke(o, new object[] { n });
        }
    }
}
Form1

 

posted @ 2018-04-06 20:26  ParanoiaApe  阅读(186)  评论(0)    收藏  举报