1.插件实现机制,动态加载DLL,遍历DLL文件,调用无参构造函数创建对象。
private void Form1_Load(object sender, EventArgs e)
{
string path = @"D:\dll";
string[] files=Directory.GetFiles(path,"*.dll");
foreach (string file in files)
{
Assembly asm = Assembly.LoadFile(file);
Type[] types = asm.GetExportedTypes();
Type type = types[0];
object obj = Activator.CreateInstance(type);
Plugwhight pw = obj as Plugwhight;
ToolStripMenuItem mi1 = new ToolStripMenuItem(pw.Name);
工具ToolStripMenuItem.DropDownItems.Add(mi1);
mi1.Click += new EventHandler(mi1_Click);
mi1.Tag = obj;
}
}
void mi1_Click(object sender, EventArgs e)
{
ToolStripMenuItem mi = sender as ToolStripMenuItem;
Plugwhight pw = (Plugwhight)mi.Tag;
textBox1.Text = pw.ReplText(textBox1.Text);
}
接口
public interface Plugwhight
{
string Name { get; }
string ReplText(string oldText);
}