Type typeInterface;//定义的接口类型
private ArrayList plugins = new ArrayList();//存放具体实现类
int counts = 0;//编号(索引)
private void Form1_Load(object sender, EventArgs e)
{
InterfaceByNote editor;//接口类型
this.FormBorderStyle = FormBorderStyle.FixedDialog;//固定窗体
//先获取程序集目录路径
//string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Addons");
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Addons");
//string[] interpath = Directory.GetFiles(path, "*.dll");
string[] interpath = Directory.GetFiles(path, "*.dll");
for (int i = 0; i < interpath.Length; i++)//循环每个dll文件
{
//Assembly asm = Assembly.LoadFile(interpath[i]);
Assembly asm = Assembly.LoadFile(interpath[i]);//加载每个程序及
//获取程序集里的所有公共类
Type[] types = asm.GetExportedTypes();
typeInterface = typeof(InterfaceByNote);//获取接口的type
for (int r = 0; r < types.Length; r++)
{
//循环遍历每个类,判断当前类是否实现了接口并且那不是抽象类
if (typeInterface.IsAssignableFrom(types[r]) && !types[r].IsAbstract)
{
editor = (InterfaceByNote)Activator.CreateInstance(types[r]);//创建该实现了接口的类
ToolStripItem T = this.编辑EToolStripMenuItem.DropDownItems.Add(editor.Name.ToString());
plugins.Add(editor);//添加到实现类的集合中
T.Tag = counts;
counts++;
T.Click += T_Click1;
}
}
}
}
/// <summary>
/// 扩展插件点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void T_Click1(object sender, EventArgs e)
{
ToolStripItem s = sender as ToolStripItem;//转换成ToolStripItem -> tag
InterfaceByNote st = (InterfaceByNote)plugins[Convert.ToInt32(s.Tag)];//根据tag属性 去取 对应实现类
st.Run(this.textBox1);//执行对应的实现类方法
}
反射练习-文本编辑扩展插件
猜你喜欢
转载自blog.csdn.net/MrLsss/article/details/104642967
今日推荐
周排行