winform插件机制学习
这两天在看自定义控件,原来有太多知识没有掌握。今天看到插件机制,心里突然一亮,这个东西听了不少次,就是不知道是啥回事。这次有幸书里包含一个案例,我就跟着它一步步来。终于知道是什么回事了。这个应该在软件开发中非常多见。只是当时不理解罢了。
开始
新建一个winform项目CustomControls
在窗体上放一个button按钮
窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Plugin;
namespace CustomControls
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnok_Click(object sender, EventArgs e)
{
CreatePlugInDockForm("", "Plugin.Form1", "Plugin");
}
public static void CreatePlugInDockForm(string strPlugInSubFilePath, string strFormFullName, string strAssemblyName)
{
try
{
//项目的Assembly选项名称DockSample.dll 即 DockSample
string path = strAssemblyName;
//类的全路径名称=> “DockSample.Modules.frm班组日志管理”
string name = strFormFullName;
string currentDirectory =
System.IO.Directory.GetCurrentDirectory();
if (strPlugInSubFilePath == "")
{
currentDirectory = currentDirectory + "\\PlugIns";
}
else
{
//同样的dll,可以按照打上客户的名称,然后加载对应客户的dll,这样就可以防止冲突
currentDirectory = currentDirectory + "\\PlugIns" + "\\" +
strPlugInSubFilePath;
}
string[] dllFilenames = System.IO.Directory.GetFiles(currentDirectory,
strAssemblyName + ".dll");
if (dllFilenames.Length == 1)
{
Assembly asm = Assembly.LoadFrom(dllFilenames[0]);
Form frm = (Form)asm.CreateInstance(strFormFullName, true);
//实现PlugIn.ICMFormPlugIn接口
if (frm.GetType().GetInterface(typeof(ICMFormPlugIn).FullName) != null)
{
frm.Show();
}
else
{
MessageBox.Show("没有实现接口");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
再新建一个Plugin类库项目
加一个接口ICMFormPlugIn
接口代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Plugin
{
public interface ICMFormPlugIn
{
void Show();
void Close();
}
}
在里面再新建一个form1窗体,并继承接口。再拖一个按钮控件
窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Plugin
{
public partial class Form1 : Form,ICMFormPlugIn
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("hello wo shi chajian");
}
}
}
第一个项目要引用后面插件项目,不然在实现接口调用时错误,这个地方
//实现PlugIn.ICMFormPlugIn接口
if (frm.GetType().GetInterface(typeof(ICMFormPlugIn).FullName) != null)
其实也可以直接写
frm.GetType().GetInterface("ICMFormPlugIn")
在CustomControls项目bin里新建一个文件夹,把生成的Plugin.dll文件放入其中。然后运行就有一下效果了

算是完成了。
参考《.NET 控件开发基础》
winform插件机制学习的更多相关文章
- kubernetes CSI 插件机制学习笔记
前言 最近在极客时间订阅了kubernetes的专栏,这篇文章是想记录一下自己学习 CSI 插件机制 (container-storage-interface) 的过程,加深一下记忆. 准备工作 老师 ...
- 微信开发学习日记(八):7步看懂weiphp插件机制,核心目标是响应微信请求
又经过了几个小时的梳理.回顾,截至目前,终于对weiphp这个框架的机制搞明白了些.想要完全明白,自然还需要大把的时间.第1步: 配置微信公众号,http://weiphp.jiutianniao ...
- Omi框架学习之旅 - 插件机制之omi-finger 及原理说明
以前那篇我写的alloyfinger源码解读那篇帖子,就说过这是一个很好用的手势库,hammer能做的,他都能做到, 而且源码只有350来行代码,很容易看懂. 那么怎么把这么好的库作为omi库的一个插 ...
- Maven生命周期和插件机制
Maven中的一个非常重要的概念是生命周期和插件,这篇文章重点介绍下Maven的生命周期. Maven的生命周期是抽象的,具体的功能是有具体的插件来完成的,Maven有相当多的功能插件,以至于Mave ...
- [转]仿World Wind构造自己的C#版插件框架——WW插件机制精简改造
很久没自己写东西啦,早该好好总结一下啦!一个大师说过“一个问题不应该被解决两次!”,除了一个好脑筋,再就是要坚持总结. 最近需要搞个系统的插件式框架,我参照World Wind的插件方式构建了个插件框 ...
- 探寻 webpack 插件机制
webpack 可谓是让人欣喜又让人忧,功能强大但需要一定的学习成本.在探寻 webpack 插件机制前,首先需要了解一件有意思的事情,webpack 插件机制是整个 webpack 工具的骨架,而 ...
- MyBatis 源码分析 - 插件机制
1.简介 一般情况下,开源框架都会提供插件或其他形式的拓展点,供开发者自行拓展.这样的好处是显而易见的,一是增加了框架的灵活性.二是开发者可以结合实际需求,对框架进行拓展,使其能够更好的工作.以 My ...
- mybatis的插件机制
一.mybatis的插件介绍 关于mybatis的插件,我想大家也都用过,就比如最常用的逆向工程,根据表结构生成model,dao,xml文件,还有分页插件,那这些插件的工作原理是怎么样的呢,就比如分 ...
- 精尽MyBatis源码分析 - 插件机制
该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...
随机推荐
- Point Grey FlyCapture 实例汇总
Example Language Description AsyncTriggerEx C++ Demonstrates some of the basic asynchronous trigger ...
- JSP + AJAX完整实例及代码
(1)发送请求index.jsp,注意引入jquery.min.js文件 <%@ page language="java" contentType="text/ht ...
- [转]理解Linux系统中的load average
转自:http://heipark.iteye.com/blog/1340384 谢谢,写的非常好的文章. 一.什么是load average linux系统中的Load对当前CPU工作量的度量 (W ...
- Centos上Docker 使用dockerfile构建容器实现ssh
这几日在学习docker.遇到的问题数一年都数不完,网上大多数都是ubuntu的,百度或者谷歌的时候心好累.写写文档来帮助使用centos的docker爱好者们. docker基本操作这里就不介绍了 ...
- PowerDesigner 15.2入门学习 一
好久没有搞 PowerDesigner 然后记录一下 1.下载地址 http://download.sybase.com/eval/PowerDesigner/PowerDesigner152_Eva ...
- IOS彩票第三天界面
******ios6 和ios7的适配 ILBaseTableViewController.m - (void)viewDidLoad { [super viewDidLoad]; // 244 24 ...
- venus java高并发框架
http://www.iteye.com/topic/1118484 因为有 netty.mima等远程框架.包括spring jboss等remoting框架 和阿里的dubbo相比, 没有亮点.. ...
- Solved Unable to copy the source file ./installer/services.sh to the destination file /etc/vmware-t
Sometimes when you intall vmwaretools there will be some problems such as "Unable to copy the s ...
- A trip through the Graphics Pipeline 2011_11 Stream Out
Welcome back! This time, the focus is going to be on Stream-Out (SO). This is a facility for storing ...
- ThinkPHP 3.2.3 自动加载公共函数文件的方法
方法一.加载默认的公共函数文件 在 ThinkPHP 3.2.3 中,默认的公共函数文件位于公共模块 ./Application/Common 下,访问所有的模块之前都会首先加载公共模块下面的配置文件 ...