记得有个winform利用反射做的可扩展笔记本,闲来无事,便用wpf也搞了个可扩展记事本,可用接口动态扩展功能,较简单,以便参考:
目录结构如下:
MainWindow.xaml为主功能界面,FunctionExpand提供扩展接口;
主要功能代码如下:
private void MakeFun()
{
string location = this.GetType().Assembly.Location;
string dllPath = System.IO.Path.GetDirectoryName(location) + "\\plugs";
string[] strDllPath = System.IO.Directory.GetFiles(dllPath, "*.dll");
foreach (var item in strDllPath)
{
Assembly ass = Assembly.LoadFile(item);
Type[] types = ass.GetExportedTypes();
Type plugType = typeof(IPlug);
foreach (var t in types)
{
//判断是否实现IPlug接口
if (plugType.IsAssignableFrom(t))
{
//根据插件类型创建插件对象
IPlug iplugObj = Activator.CreateInstance(t) as IPlug;
//创建插件按钮
MenuItem menu = new MenuItem();
menu.Header = t.Name;
this.expandTool.Items.Add(menu);
menu.Click += menu_Click;
menu.Tag = iplugObj;
}
}
}
}
void menu_Click(object sender, RoutedEventArgs e)
{
MenuItem menuItem = sender as MenuItem;
IPlug iplug = menuItem.Tag as IPlug;
tb_Text.Text = iplug.ProcessText(tb_Text.Text);
}
效果如下:
现把代码分享出来,有感兴趣的朋友可以研究下: 源码下载
转载请注明来自:闪闪的幸运星
原文地址:http://www.cnblogs.com/dongyang
如若转载,请保留原文地址。谢谢合作。