public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//主程序的加载事件
//1.获取plugins目录下的所有dll文件。
//1.1获取plugins目录的完整路径
string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\plugins";
//1.2搜索dir目录下的所有的dll文件
string[] dlls = Directory.GetFiles(dir, "*.dll");
//1.3循环每个dll文件,并加载
foreach (string dll in dlls)
{
//通过Assembly动态加载每个dll文件
Assembly asm = Assembly.LoadFile(dll);
//获取插件中的那些public的类型
Type[] types = asm.GetExportedTypes();
//获取接口的Type
Type typeIEditor = typeof(IEditor);
//循环判断每个类型是否实现了IEditor接口,并且判断该类型是否不是抽象的。
for (int i = 0; i < types.Length; i++)
{
//判断当前的类型types[i]是否实现了接口IEditor,并且当前类型不是接口或抽象类。
if (typeIEditor.IsAssignableFrom(types[i]) && !types[i].IsAbstract)
{
//获取当前类型的Name属性中的值,然后将该值加载到菜单栏上
PropertyInfo pinfo = types[i].GetProperty("Name");
//创建了当前类型的一个对象。
object obj = Activator.CreateInstance(types[i]);
//objVal就是Name属性中的值。
object objVal = pinfo.GetValue(obj, null);//调用索引器的时候才需要第二个参数。
//将objVal中的值显示到菜单栏上。
ToolStripItem tsi = msTools.DropDownItems.Add(objVal.ToString());
//为当前菜单项注册一个单击事件
tsi.Click += new EventHandler(tsi_Click);
//将当前插件中的对象存储到菜单栏的Tag中。
tsi.Tag = obj;
}
}
}
}
void tsi_Click(object sender, EventArgs e)
{
ToolStripItem item = (ToolStripItem)sender;
IEditor editor = (IEditor)item.Tag;
editor.Execute(this.textBox1);
}
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//主程序的加载事件
//1.获取plugins目录下的所有dll文件。
//1.1获取plugins目录的完整路径
string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\plugins";
//1.2搜索dir目录下的所有的dll文件
string[] dlls = Directory.GetFiles(dir, "*.dll");
//1.3循环每个dll文件,并加载
foreach (string dll in dlls)
{
//通过Assembly动态加载每个dll文件
Assembly asm = Assembly.LoadFile(dll);
//获取插件中的那些public的类型
Type[] types = asm.GetExportedTypes();
//获取接口的Type
Type typeIEditor = typeof(IEditor);
//循环判断每个类型是否实现了IEditor接口,并且判断该类型是否不是抽象的。
for (int i = 0; i < types.Length; i++)
{
//判断当前的类型types[i]是否实现了接口IEditor,并且当前类型不是接口或抽象类。
if (typeIEditor.IsAssignableFrom(types[i]) && !types[i].IsAbstract)
{
//获取当前类型的Name属性中的值,然后将该值加载到菜单栏上
PropertyInfo pinfo = types[i].GetProperty("Name");
//创建了当前类型的一个对象。
object obj = Activator.CreateInstance(types[i]);
//objVal就是Name属性中的值。
object objVal = pinfo.GetValue(obj, null);//调用索引器的时候才需要第二个参数。
//将objVal中的值显示到菜单栏上。
ToolStripItem tsi = msTools.DropDownItems.Add(objVal.ToString());
//为当前菜单项注册一个单击事件
tsi.Click += new EventHandler(tsi_Click);
//将当前插件中的对象存储到菜单栏的Tag中。
tsi.Tag = obj;
}
}
}
}
void tsi_Click(object sender, EventArgs e)
{
ToolStripItem item = (ToolStripItem)sender;
IEditor editor = (IEditor)item.Tag;
editor.Execute(this.textBox1);
}