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 ) ;
}
}
查看全文
相关阅读:
[C++] split string by string
工作三个月心得经验
Ubuntu Command-Line: Enable Unlimited Scrolling in the Terminal
What is the PPA and How to do with it ?
WCF vs ASMX WebService
The ShortCuts in the ADT (to be continued)
when does the View.ondraw method get called
Browsing Storage Resources with Server Explorer
Get start with Android development
C++ Frequently asking question
原文地址:https://www.cnblogs.com/tonybinlj/p/1297904.html
最新文章
Notepad++快捷键&正则表达式替换字符串&插件
Discuz有关问题解决办法汇总
SQL Server 2008 数据库镜像部署实例之三 配置见证服务器
SQL Server 2008 数据库镜像部署实例之二 配置镜像,实施手动故障转移
SQL Server 2008 数据库镜像部署实例之一 数据库准备
SQL Server 2008 数据库通过镜像同步备份(数据库热备)
SQL Server复制需要有实际的服务器名称才能连接到服务器
server application error应用错误
SQLLDR 教程
Castle DynamicProxy
热门文章
Non-English query
Executing PowerShell scripts from C#
Inside of Asp.net web form
Base64 Converter
关于asp.net impersonation的一些谣传
Round in Oracle/VBA
XSS in asp.net
Asp.net web hosting
Jquery中的(function($){...})(jQuery)
C#一探究竟——枚举
Copyright © 2011-2022 走看看