C# 插件开发
2015-08-30 14:38
417 查看
//1定义插件接口,将其编译成dll,例如:
using
System;
namespace
PluginInterface
{
public
interface
IShow
{
string
Show();
}
}
//2编写插件.新建dll工程,并引用第一步做的dll插件,实现其接口,例如:
namespace
PluginA
{
public
class
PluginA:PluginInterface.IShow
{
public
string
Show()
{
return
"IampluginA"
;
}
}
}
//3.在指定目录下寻找Dll文件
private
void
frmMain_Load(
object
sender,System.EventArgse)
{
//获取Plugins目录中所有的DLL文件,并保存在combo中
try
{
string
path=Application.StartupPath;
path=System.IO.Path.Combine(path,
"Plugins"
);
foreach
(
string
file
in
System.IO.Directory.GetFiles(path,
"*.dll"
))
{
this
.cmbPlugins.Items.Add(file);
}
}
catch
(Exceptionex)
{
MessageBox.Show(ex.Message);
}
}
//使用插件
privatevoidbtnExecute_Click(objectsender,System.EventArgse)
{
try
{
//1.获得文件名称
stringasmFile=this.cmbPlugins.Text;
stringasmName=System.IO.Path.GetFileNameWithoutExtension(asmFile);
if(asmFile!=string.Empty)
{
//2.利用反射,构造DLL文件的实例
System.Reflection.Assemblyasm=System.Reflection.Assembly.LoadFrom(asmFile);
//3.利用反射,从程序集(DLL)中,提取类,并把此类实例化
PluginInterface.IShowiShow=(PluginInterface.IShow)
System.Activator.CreateInstance(asm.GetType(asmName+"Namespace."+asmName+"Class"));
//4.在主程序中使用这个类的实例
this.label2.Text=iShow.Show();
}
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message);
}
}
相关文章推荐
- C# string类型和byte[]类型相互转换
- BC#53 1001 Rikka with Graph
- C#中的delegate与event简单讲解
- C#中堆和栈的区别分析
- C#基础案例------聊天机器人
- c#之内置类型
- C# WindowsEvent Select
- c# TextBox只允许输入数字,禁用右键粘贴,允许Ctrl+v粘贴数字
- C# - 委托的使用
- c#输出文件对话框
- c#格式化数字
- winform,C#,打开文件对话框的使用
- C# - 重写虚方法
- C# - 使用 List<> 泛型给GridView控件数据
- C#基础------字符串函数
- C# ref 和 out ,params关键字的用法
- CSharpGL(0)一个易学易用的C#版OpenGL
- ICSharpCode.SharpZipLib.dll 使用方法
- this(C#)
- this(C#)