码迷,mamicode.com
首页 > 其他好文 > 详细

反射的方法操作记事本添加插件

时间:2015-11-18 22:54:40      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:

本文主要讲的是通过反射动态获取插件。

 

首先所有的插件必须遵守规范,这个规范是一个接口,定义如下:

1  public interface IEditPlus//interface一定要加public,否则外部无法引用
2     {
3         string Name { get; }//插件的名字
4         void ChangeString(TextBox tb);//改变字符串的方法
5     }

主窗口的界面

技术分享

主要的思路是查找/debug/lib文件夹下的所有的.dll文件,通过反射的方法对其进行操作。
(在菜单编辑下添加子菜单)
 1   private void Form1_Load(object sender, EventArgs e)
 2         {
 3             //搜索/bin/*.dll
 4             //获取当前程序集
 5             Assembly ass = Assembly.GetExecutingAssembly();
 6             string dirPath = Path.Combine(Path.GetDirectoryName(ass.Location), "lib");
 7             string[] filePaths = Directory.GetFiles(dirPath, "*.dll");//在当前程序集的文件夹中搜索所有的dll文件
 8             foreach (var filePath in filePaths)
 9             {
10                 Assembly assdll = Assembly.LoadFile(filePath);
11                 Type[] tps = assdll.GetTypes();
12                 foreach (Type tp in tps)
13                 {
14                     if (typeof(IEditPlus).IsAssignableFrom(tp) && !tp.IsAbstract)
15                     {
16                         IEditPlus ep = (IEditPlus)Activator.CreateInstance(tp);//获取对象转换为接口对象
17                         ToolStripItem tsi = tms.DropDownItems.Add(ep.Name);//为编辑菜单加载子菜单
18                         tsi.Tag = ep;//将这个对象作为tsi的Tag保存
19                         tsi.Click += tsi_Click;//为这个子列表添加单击事件
20                     }
21                 }
22             }
23         }
24         void tsi_Click(object sender, EventArgs e)
25         {
26             ToolStripItem tsi = sender as ToolStripItem;//将sender显示转换为toolstripitem
27             if (tsi!=null)//要判断下tsi不为null,因为sender是一个接受的对象
28             {
29                 //在事件中要操作IEditPlus的ChangeString方法,所以要从tag中取出
30                 IEditPlus ep = tsi.Tag as IEditPlus;
31                 ep.ChangeString(textBox1);
32             }
33          
34         }

小写转大写的dll文件代码---将生成的dll代码放在主程序的/debug/lib文件下

 1 public class 小写转大写:IEditPlus
 2     {
 3         public void ChangeString(System.Windows.Forms.TextBox tb)
 4         {
 5             tb.Text = tb.Text.ToUpper();
 6         }
 7         public string Name
 8         {
 9             get { return "小写转大写"; }
10         }
11     }

改变字体的dll代码---将生成的dll代码放在主程序的/debug/lib文件下

先添加一个窗体,界面

技术分享

添加类的代码:

1 public void ChangeString(System.Windows.Forms.TextBox tb)
2         {
3             FontFm ff = new FontFm(tb);
4             ff.ShowDialog();
5         }
6         public string Name
7         {
8             get { return "字体"; }
9         }

窗体的代码:

 1  public FontFm()
 2         {
 3             InitializeComponent();
 4         }
 5         public FontFm(TextBox tb):this()
 6         {
 7            this._tb = tb; 
 8         }
 9         private TextBox _tb;//要操作TextBox tb添加字段和构造函数
10         private void button1_Click(object sender, EventArgs e)
11         {
12             _tb.Font = new Font(cbmFont.Text, float.Parse(cbmSize.Text));//改变字体
13             this.Close();//关闭窗体
14         }

 

我自己觉得,反射和接口一起用,棒棒哒!!!

 

反射的方法操作记事本添加插件

标签:

原文地址:http://www.cnblogs.com/lucyliang/p/4975935.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!