首页 > 其他 > 详细

用反射开发控件

时间:2016-01-28 18:41:12      阅读:202      评论:0      收藏:0      [点我收藏+]
l创建接口项目,通过接口定义插件怎么去实现
l创建插件项目,创建两个插件一个实现汉英翻译功能,一个实现定时关机功能。插件项目要引用接口项目。编译时让插件项目生成在主程序的debug/plugin目录下
l创建主程序,主程序要添加对接口项目的引用(不需要对插件引用,对插件的调用是动态的)
•主程序中读取Plugin目录下的所有dll文件,加载成Assembly
•Ass.GetExportedTypes读取Assembly中的公共类型
•IsAssignableFrom判断对象能否委派给某类型,是否是类并且不能使抽象类
•动态添加菜单
 
实例,创建一个记事本,在格式下拉单下动态增加一个菜单
技术分享
 
记事本窗体项目是NotPad
记事本留出的可拓展控件项目是NotePadInfer,接口名字是ExtInfo
 
控件类ExtControl
这里技术分享控件继承接口,引用接口项目
 
窗体项目引用接口
技术分享
 
 
NotePad的代码
技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 using System.IO;
10 using System.Reflection;
11 using NotePadInfer;
12 
13 namespace NotePad
14 {
15     public partial class Form1 : Form
16     {
17         public Form1()
18         {
19             InitializeComponent();
20         }
21 
22         private void Form1_Load(object sender, EventArgs e)
23         {
24 
25             //找到路径
26             string exepath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "add");
27 
28 
29             //find all dll
30             string[] files = Directory.GetFiles(exepath, "*.dll");
31             Type extInterface = typeof(NotePadInfer.ExtInfo);//接口类型
32 
33 
34             foreach (string file in files)
35             {
36                 // load  dll
37                 Assembly assemly = Assembly.LoadFrom(file);
38                 //find all public type
39                 Type[] extTypes = assemly.GetExportedTypes();
40                 //foreach types 
41                 foreach (Type item in extTypes)
42                 {
43                     //判断item是否是实现了接口extInterface
44                     if (extInterface.IsAssignableFrom(item) && !item.IsAbstract)
45                     { 
46                         ///实例化类,用实例来调方法和属性
47                         ExtInfo instanceInterface = (ExtInfo)Activator.CreateInstance(item);
48                        //向菜单栏中动态添加一个菜单项
49                         ToolStripItem toolItem = this.格式ToolStripMenuItem.DropDownItems.Add(instanceInterface.Name);
50                         toolItem.Tag = instanceInterface;//把实例赋给Tag
51                         //注册加载的控件的事件
52                         toolItem.Click += new EventHandler(toolItem_Click);
53                     }
54                 }
55 
56             }
57         }
58 
59         void toolItem_Click(object sender, EventArgs e)
60         {
61           //sender这里是下拉单
62             ToolStripItem strip  = sender as ToolStripItem;
63             if (strip!=null)
64             {
65                 if (strip.Tag!=null)
66                 {
67                     ExtInfo t = strip.Tag as ExtInfo;
68                     t.Run(textBox1);
69                 }
70             }
71         }
72 
73     
74 
75       
76 
77 
78     }
79 }
View Code

NoteInfo接口中的代码

技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Windows.Forms;
 6 
 7 namespace NotePadInfer
 8 {
 9    public  interface ExtInfo
10     {
11        //插件的名字
12       string Name
13         {
14             get;
15         }
16 
17         //插件的运行 插件修改的是textbox
18          void Run(TextBox text);
19 
20 
21        
22     }
23 }
View Code

控件的代码

技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using NotePadInfer;
 6 using System.Windows.Forms;
 7 
 8 
 9 namespace NotePadExtControl
10 {
11     public class ExtControl:ExtInfo
12     {
13 
14         public string Name
15         {
16             get { return "To Upper" ; }
17         }
18 
19       
20 
21         public void Run(TextBox text)
22         {
23             text.Text = text.Text.ToUpper();
24         }
25     }
26 }
View Code

 

用反射开发控件

原文:http://www.cnblogs.com/nanxiaoxiang/p/5166850.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!