C# .NET 插件开发
2008-01-11 17:22
375 查看
//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 "I am plugin A";
}
}
}
2. 在主程序中收集或载入插件
//3. 在指定目录下寻找Dll文件
private void frmMain_Load(object sender, System.EventArgs e)
{
//获取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 (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
3. 使用插件
private void btnExecute_Click(object sender, System.EventArgs e)
{
try
{
//1. 获得 文件名称
string asmFile = this.cmbPlugins.Text;
string asmName = System.IO.Path.GetFileNameWithoutExtension(asmFile);
if (asmFile != string.Empty)
{
//2. 利用反射,构造DLL文件的实例
System.Reflection.Assembly asm = System.Reflection.Assembly.LoadFrom(asmFile);
//3. 利用反射,从程序集(DLL)中,提取类,并把此类实例化
PluginInterface.IShow iShow = (PluginInterface.IShow)
System.Activator.CreateInstance(asm.GetType(asmName + "Namespace." + asmName + "Class"));
//4. 在主程序中使用这个类的实例
this.label2.Text = iShow.Show();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
using System;
namespace PluginInterface
{
public interface IShow
{
string Show();
}
}
//2 编写插件. 新建dll工程,并引用第一步做的dll插件,实现其接口,例如:
namespace PluginA
{
public class PluginA : PluginInterface.IShow
{
public string Show()
{
return "I am plugin A";
}
}
}
2. 在主程序中收集或载入插件
//3. 在指定目录下寻找Dll文件
private void frmMain_Load(object sender, System.EventArgs e)
{
//获取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 (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
3. 使用插件
private void btnExecute_Click(object sender, System.EventArgs e)
{
try
{
//1. 获得 文件名称
string asmFile = this.cmbPlugins.Text;
string asmName = System.IO.Path.GetFileNameWithoutExtension(asmFile);
if (asmFile != string.Empty)
{
//2. 利用反射,构造DLL文件的实例
System.Reflection.Assembly asm = System.Reflection.Assembly.LoadFrom(asmFile);
//3. 利用反射,从程序集(DLL)中,提取类,并把此类实例化
PluginInterface.IShow iShow = (PluginInterface.IShow)
System.Activator.CreateInstance(asm.GetType(asmName + "Namespace." + asmName + "Class"));
//4. 在主程序中使用这个类的实例
this.label2.Text = iShow.Show();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
相关文章推荐
- C# .NET 插件开发
- 用Visual Studio 2005 开发VB.NET-C#语言转换插件
- Web图片上传插件开发文档-ASP.NET(C#)
- C# .NET 插件开发
- 实现用VB.Net/(C#)开发K/3 BOS 插件的真正可行方法
- 圣殿祭司:ASP.NET 2.0 开发详解——使用C#——全国购书地址一览表
- Asp.net &C#开发中的一些注意事项及小技巧
- C#.NET 大型通用信息化系统集成快速开发平台 4.0 版本 - 独立子系统管理员功能实现
- [C#.net]操作系统作业-模拟二级文件系统-开发日志
- .NET MVC插件化开发框架源码(插件功能完善版)
- C#.NET常见问题(FAQ)-使用SharpDevelop开发 如何切换设计视图和代码视图
- 今天才明白VC++.net的含义: VS系列的c++编译器可以支持托管C++(类似于C#,具体请看MS在线文档),当然包括winform界面开发。
- C#开发的类库dll,注册为Arcgis的插件tlb的方法
- 使用C# .net开发微信公众号之服务器URL配置
- asp.net+C# 开发网页区域更新的方法 .(转帖)
- asp.net(c#)开发中的文件上传组件uploadify的使用方法(带进度条)
- Visual C#.Net 网络程序开发-Socket篇
- C#.net开发 List与DataTable相互转换
- C#报表开发工具ReportViewer vs Stimulsoft Reports.Net
- 【推荐】利用vs.net快速开发windows服务(c#)