zoukankan
html css js c++ java
开发c#插件步骤
开发c#插件步骤
//
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
"
;
}
}
}
收集程序集:
//
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 ) ;
}
}
使用插件
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 ) ;
}
}
查看全文
相关阅读:
python列表--查找集合中重复元素的个数
python3-打印一个进度条
python3-sys模块
python3-字符串操作
python3-深浅复制
python3-os模块
接口和抽象类有什么区别
集合知识
面向对象的特征有哪些方面
javadoc时候乱码-编码 GBK 的不可映射字符
原文地址:https://www.cnblogs.com/tonybinlj/p/1297904.html
最新文章
iview2.0 日期选择器DatePicker 所选时间格式不对
【java工具类】对字节数组字符串进行Base64解码并生成图片
【java工具类】AES加密解密
SpringBoot+Mybatis-Plus两种分页方法
Linux使用httpd配置反代理
Linux安装httpd
Python re模块
Python 正则
python json模块
Python sys 模块
热门文章
python commands 模块
python os 模块
Python logging 模块
python time模块和datetime模块
python 函数、模块、包及import导入方法
python 异常
python3-递归
python3--高阶函数
python3-函数的参数的四种简单用法:
python中的enumerate函数
Copyright © 2011-2022 走看看