|
发表于 2019-6-15 13:14:04
|
显示全部楼层
江苏省南京市
Dictionary<string, Func<string, string>> DicInit();
}
//dll
namespace EditExtPlugIntest{
public class EditExt : IEditExt{
public string Title => "插件1";
Dictionary<string, Func<string, string>> dic = new Dictionary<string, Func<string, string>>();
Func<string, string> func ;
public Dictionary<string, Func<string, string>> DicInit(){
dic.Add("功能1", func = str => str + "功能1");
dic.Add("功能2", func = str => str + "功能2");
return dic;
}
}
}
//程序
public partial class FrmPlugin : Form{
public FrmPlugin(){
InitializeComponent();
}
Dictionary<string, Func<string, string>> dic;
private void FrmPlugin_Load(object sender, EventArgs e){
string[] files = Directory.GetFiles(Application.StartupPath, "*.dll");
foreach (var file in files){
Assembly ass = Assembly.LoadFile(file);
Type[] types = ass.GetTypes();//获取程序集中所有类信息
foreach (var type in types){
if (typeof(IEditExt).IsAssignableFrom(type)){
ConstructorInfo ct = type.GetConstructor(new Type[0]);
IEditExt editex = (IEditExt)ct.Invoke(new object[0]);
ToolStripMenuItem item = new ToolStripMenuItem();//动态创建一级菜单
mitemPlugin.DropDownItems.AddRange(new ToolStripItem[] {item});
item.Text = editex.Title;
dic = editex.DicInit();//获取委托
foreach (var ditem in dic){
ToolStripMenuItem item1 = new ToolStripMenuItem();//动态创建二级菜单
item.DropDownItems.AddRange(new ToolStripItem[] { item1 });
item1.Text = ditem.Key;
item1.Click += Item1_Click;
item1.Tag = editex;
}
}
}
}
}
private void Item1_Click(object sender, EventArgs e){//事件
string str = sender.ToString();
string str1 = dic[str]("abcdefg");
MessageBox.Show(str1);
}
}
现在加上委托,调用dll已知的程序加载函数就能将插件中所有方法加载到窗口上,这样能随意调用dll中任何定义好的函数,这样的插件还有点意思
|
评分
-
查看全部评分
|