C#插件入门

(一)建立接口类库IDialog,添加接口文件IShowDialog.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
 
namespace IDialog
{
    public interface IShowDialog
    {
        void ShowMyDialog();
 
    }
}

(二)新建插件,添加类库Dialog,实现接口IDalog类Dialog,添加IDialog接口类库的引用就不必多说了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IDialog;
 
namespace Dialog
{
    public class Dialog:Form,IShowDialog
    {
 
        public void ShowMyDialog()
        {
            Form frmMyForm = new Form();
            frmMyForm.Text = "Hello World";
            frmMyForm.Show();
        }
    }
}


(三)建立Form应用程序,装载插件Dialog,并显示插件,添加ListBox用来显示插件的名字,
两个Button控件,btnLoadPlugs用来装载插件,btnRunPlugs用来运行插件

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.Collections;
using System.Reflection;
using System.IO;
 
 
namespace MainInvokePlugins
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }
 
 
        private ArrayList listPlugs = new ArrayList();
        private void btnLoadPlugs_Click(object sender, EventArgs e)         {             string []files= Directory.GetFiles(Application.StartupPath);             try             {                 foreach (string item in files)                 {                     if (item.ToUpper().EndsWith(".DLL"))                     {                         Assembly a = Assembly.LoadFile(item);                         Type[] ts = a.GetTypes();                         foreach (Type t in ts)                         {                             if (t.GetInterface("IShowDialog") != null)                             {                                 listPlugs.Add(a.CreateInstance(t.FullName));                                 this.listNames.Items.Add(a.FullName);                                 ;                             }                         }                     }                 }             }             catch (Exception ex)             {                 MessageBox.Show(ex.Message);                                      }                                                    }         private void btnRunPlugs_Click(object sender, EventArgs e)         {             if (this.listNames.SelectedIndex==-1)             {                 return;             }             else             {                 try                 {                     object plug = this.listPlugs[this.listNames.SelectedIndex];                     Type t = plug.GetType();                     MethodInfo showDialog = t.GetMethod("ShowMyDialog");                     showDialog.Invoke(plug, null);                 }                 catch (Exception ex)                 {                     MessageBox.Show(ex.Message);                 }             }         }     } }



 

转载于:https://www.cnblogs.com/ganquanfu2008/archive/2013/04/14/3020665.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值