1.定义插件接口,将其编译为DLL
namespace PluginInterface
{
public interface IShow
{
string show();
}
}
2 .编写插件,引用上面的DLL,实现上面定义的接口,也编译为DLL
//插件A
namespace PluginInterface
{
public class PluginA:IShow
{
public string show()
{
return "插件A";
}
}
}
//插件B
namespace PluginB
{
public class PluginB:IShow
{
public string show()
{
return "插件B";
}
}
}
3,在程序中使用插件,需要引用定义插件接口的DLL
namespace testPlugin
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//查找所有的插件的路径
private List<string> FindPlugin()
{
List<string> PluginPaths = new List<string>();
try
{
//获取当前程序的启动路径
string path = Application.StartupPath;
//合并路径,指向插件所在的目录
path = System.IO.Path.Combine(path, "Plugins");
foreach (string fileName in System.IO.Directory.GetFiles(path, "*.dll"))
{
PluginPaths.Add(fileName);
&nbs