通过反射加载DLL部分更新应用程序(winform)
来源:互联网 发布:腾讯数据恢复留言 编辑:程序博客网 时间:2024/05/18 00:29
方法:加载dll,再界面创建控件,注册控件的点击事件
namespace WindowsFormsApplication2
{
public partial class Form1 :Form
{
public Form1()
{
InitializeComponent();
Type[] typePublic = null; Type typeIExecute = null;
string exeDirPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); //得到运行目录
string[] dellPaths = Directory.GetFiles(exeDirPath, "*.dll");
for (int i = 0; i < dellPaths.Length; i++)
{
Assembly asm = Assembly.LoadFile(dellPaths[i]);
//Assembly asm = Assembly.LoadFrom("ClassLibrary1.dll");
typePublic = asm.GetExportedTypes();//得到所有的public方法
typeIExecute = typeof(IExecute);//接口的类型
for (int j = 0; j < typePublic.Length; j++)
{
if (typeIExecute.IsAssignableFrom(typePublic[j]) && !typePublic[j].IsAbstract)//实现了IExecute,且非抽象类
{
IExecute execut = (IExecute)Activator.CreateInstance(typePublic[j]);
ToolStripItem tsoitem = menuStrip1.Items.Add(execut.ExeName);//注册菜单
tsoitem.Click += new EventHandler(tsoitem_Click);//添加点击事件
tsoitem.Tag = execut;//将namespace.class记录在新增控件的tag属性中
}
}
}
}
void tsoitem_Click(object sender, EventArgs e)
{
IExecute execut = (IExecute)((ToolStripItem)sender).Tag;
execut.Execute();
}
}
public interface IExecute
{
string ExeName
{
get;
set;
}
///<summary>
///插件执行的方法
///</summary>
void Execute();
}
}
system.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication2;
[csharp] view plain copy 在CODE上查看代码片派生到我的代码片
namespace ClassLibrary1
{
public class One : IExecute
{
public One()
{
ExeName = "MenuOne";
}
public string ExeName
{
get;
set;
}
public void Execute()
{
MessageBox.Show("Execute One");
}
}
}
第二个DLL
[csharp] view plain copy 在CODE上查看代码片派生到我的代码片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WindowsFormsApplication2;
using System.Windows.Forms;
namespace ClassLibrary2
{
public class Two:IExecute
{
public Two()
{
ExeName = "MenuTwo";
}
public string ExeName
{
get;
set;
}
public void Execute()
{
MessageBox.Show("Execute ClassTwo");
}
}
}
- 通过反射加载DLL部分更新应用程序(winform)
- 通过反射动态加载DLL
- C#通过反射实现动态加载DLL
- 通过应用程序域加载和卸载dll
- 反射加载dll 执行方法
- c# winform实现dll加载时注册
- c# winform实现dll加载时注册
- .net Winform 应用程序的更新程序
- unity 安卓热更新代码的最新方法: 通过Mono加载新的重新编译的dll
- Winform应用程序实现加载等待(C#)
- 通过反射调用Dll中的方法
- C#动态加载DLL(反射学习(二))
- 关于反射加载DLL的问题2
- 利用反射进行动态加载dll
- 工厂模式下用反射加载dll
- c# 应用程序的.dll文件更新。
- 通过反射和AIDI获取应用程序大小
- Android通过反射方式强制退出应用程序
- 尚学堂java基础——第15、16集笔记
- 在代码中定义广播监听拔出sdcard事件
- VC6.0下更改字体(win64) oracle解锁用户
- 1022. Digital Library (30)
- 使用U盘安装XenServer6.5
- 通过反射加载DLL部分更新应用程序(winform)
- 并查集-poj-1182-食物链-带权值的并查集
- android使用Zxing库实现二维码的生成
- 外观模式(Facade)
- 还是弄个博客吧
- 二维码电子名片
- PHP学习笔记(1)
- REST调试工具
- centos7上安装oracle 12c