[架构模式] C#实现插件式开发的一个Demo分享
做winform程序,很多时候都需要用到插件式的,所以本人做了一个Demo,思路跟网上思路基本一致,现在共享出来如有需要的朋友可以下载。申明:部分代码来源于网上,当然思路也是,呵呵原理很简单:一:定义插件接口二:实现插件接口并建立不同工项目,使其在生成时生成不同的DLL
三:主程序运行时根据接口名利用反射对插件目录的DLL进行加载,加载完成后便可以使用插件接口定义的方法或属性了。下面上几张图,有兴趣的朋友可以先看看,觉得值得一看的朋友可以下载。
项目结构:http://images.cnblogs.com/cnblogs_com/xianhong/project.pngDefaultPlugin,PosPlugin两个项目均为插件,均实现了Iplugin接可Iplugin接口using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespacepublicinterface Iplugin
{
PluginInfoAttribute PluginInfo
{ get; setboolgetset;
}
Image ModulePicture
{
get;
}
Image ModulePictureEnter
{
get;
}
Image ModulePictureClick
{
getstringget;
}
Dictionary<string, EventHandler>get;
}
ILoadForm FormLoader
{
getset
用于加载插件的类 PluginLoaderusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using System.Windows.Forms;
using System.Collections;
namespacepublicstaticclasspublicstatic List<Iplugin> plugins =new List<Iplugin>privatestatic ArrayList piProperties =newprivatestaticbool IsValidPlugin(Type t)
{
bool ret =false;
Type[] interfaces =foreach( Type theInterface inif (theInterface.FullName =="WinDemo.Core.Iplugin")
{
ret =truebreak;
}
}
returnpublicstaticvoidstring[] files = Directory.GetFiles(Application.StartupPath +"\\plugin\\"int i =0;
PluginInfoAttribute typeAttribute =newforeach (string file instring ext = file.Substring(file.LastIndexOf("."if (ext !=".dll") continuetry
{
Assembly tmp = Assembly.LoadFile(file);
Type[] types =bool ok =falseforeach (Type t inif (IsValidPlugin(t))
{
Iplugin plugin = (Iplugin)tmp.CreateInstance(t.FullName);
plugins.Add(plugin);
object[] attbs = t.GetCustomAttributes(typeAttribute.GetType(), false);
PluginInfoAttribute attribute =nullforeach (object attb in attbs)
{
if (attb is PluginInfoAttribute)
{
attribute = (PluginInfoAttribute)attb;
attribute.Index = i;
i++;
ok =truebreak;
}
}
if (attribute !=null)
{
piProperties.Add(attribute);
plugin.PluginInfo = attribute;
}
elsethrownew Exception("未定义插件属性"if (ok) break;
}
}
catch (Exception err)
{
throw err;
}
}
plugins.Sort((p1, p2) =>return p2.PluginInfo.Index - p1.PluginInfo.Index;
});
}
}
}
Iplugin的实现类PosPlugin类using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using WinDemo.Core;
usingnamespace DefaultPlugin
{
{
private Dictionary<string, EventHandler> _ChildNodes =new Dictionary<string, EventHandler>private LeftNav frmLeftNav =newpublicstaticpublic Default()
{
_ChildNodes.Add("菜单五", (sender,e) =>
{
MessageBox.Show(sender.ToString());
});
_ChildNodes.Add("菜单四", (sender, e) =>
{
MessageBox.Show(sender.ToString());
});
_ChildNodes.Add("菜单三", (sender, e) =>
{
MessageBox.Show(sender.ToString());
});
_ChildNodes.Add("菜单二", (sender, e) =>
{
MessageBox.Show(sender.ToString());
});
_ChildNodes.Add("菜单一", (sender, e) =>
{
FormLoader.LoadNavFrm(frmLeftNav);
});
}
public Image ModulePicture
{
getreturn ((System.Drawing.Image)(ImageResource.Index));
}
}
public Image ModulePictureEnter
{
getreturn ((System.Drawing.Image)(ImageResource.IndexEnter));
}
}
public Image ModulePictureClick
{
getreturn ((System.Drawing.Image)(ImageResource.IndexClick));
}
}
publicstringgetreturn"首页"public Dictionary<string, EventHandler>getreturn _ChildNodes;
}
}
publicboolgetsetpublic ILoadForm FormLoader
{
getreturnset
{
Formloader = value;
}
}
public PluginInfoAttribute PluginInfo
{
getset
此图现在有两个插件运行效果如下
http://images.cnblogs.com/cnblogs_com/xianhong/pluginwin.png
页:
[1]