zoukankan      html  css  js  c++  java
  • c# winform插件

    插件接口

    namespace IMsg
    {
        ///<summary>
        /// 这是插件必须实现的接口,也是主程序与插件通信的唯一接口
        /// 换句话说,主程序只认识插件里的这些方法
        ///</summary>
        public interface IMsgPlug
        {
            /// <summary>
            /// 显示窗体
            /// </summary>
            void OnShowDlg();
    
            /// <summary>
            /// 显示信息
            /// </summary>
            /// <returns></returns>
            string OnShowInfo();
        }
    }

    插件1

    using IMsg;
    using System;
    
    namespace MYPlugin1
    {
        public class myConsole : IMsgPlug
        {
            #region IMsgPlug 成员
            public void OnShowDlg()
            {
                Console.WriteLine("控制台调用插件的OnShowDlg方法");
            }
            public string OnShowInfo()
            {
                return "myConsole";
            }
            #endregion
        }
    }

    插件2

    using IMsg;
    using System.Windows.Forms;
    
    namespace MYPlugin1
    {
        public class MYDlg : Form, IMsgPlug
        {
            #region IMsgPlug 成员
    
            public void OnShowDlg()
            {
                this.Text = "插件子窗体";
                this.ShowDialog();//调用Form的ShowDialog,显示窗体
            }
            public string OnShowInfo()
            {
                return "MyDlg";
            }
            #endregion
        }
    }

    winform调用插件

    using System;
    using System.Collections;
    using System.IO;
    using System.Reflection;
    using System.Windows.Forms;
    
    namespace MsgBoxMain
    {
        public partial class FormMain : Form
        {
            ///<summary>
            /// 存放插件的集合
            ///</summary>
            private ArrayList plugins = new ArrayList();
            public FormMain()
            {
                InitializeComponent();
            }
    
            ///<summary>
            /// 载入所有插件
            ///</summary>
            private void LoadAllPlugs()
            {
                //获取插件目录(plugins)下所有文件夹
                DirectoryInfo dirs = new DirectoryInfo(Application.StartupPath + @"Plugins");
    
                foreach (DirectoryInfo dir in dirs.GetDirectories())
                {
                    //获取文件夹下文件
                    string[] files = Directory.GetFiles(dir.FullName);
                    foreach (string file in files)
                    {
                        if (file.ToUpper().EndsWith(".DLL"))
                        {
                            try
                            {
                                //载入dll
                                Assembly ab = Assembly.LoadFrom(file);
                                Type[] types = ab.GetTypes();
                                foreach (Type t in types)
                                {
                                    //如果某些类实现了预定义的IMsg.IMsgPlug接口,则认为该类适配与主程序(是主程序的插件)
                                    if (t.GetInterface("IMsgPlug") != null)
                                    {
                                        plugins.Add(ab.CreateInstance(t.FullName));
                                        listBox1.Items.Add(t.FullName);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }
                    }
                }
            }
    
            private void btnLoadPlug_Click(object sender, EventArgs e)
            {
                LoadAllPlugs();
            }
    
            //调用插件的方法
            private void btnExecute_Click(object sender, EventArgs e)
            {
                if (this.listBox1.SelectedIndex == -1) return;
                object selObj = this.plugins[this.listBox1.SelectedIndex];
                Type t = selObj.GetType();
                MethodInfo OnShowDlg = t.GetMethod("OnShowDlg");
                MethodInfo OnShowInfo = t.GetMethod("OnShowInfo");
    
                OnShowDlg.Invoke(selObj, null);
                object returnValue = OnShowInfo.Invoke(selObj, null);
                this.lblMsg.Text = returnValue.ToString();
    
            }
        }
    }

    界面

  • 相关阅读:
    Java并发容器总结
    ReentrantLock源码分析
    webpack插件之webpack-dev-server
    webpack插件之htmlWebpackPlugin
    webpack配置之webpack.config.js文件配置
    webpack的安装和运行
    webpack安装大于4.x版本(没有配置webpack.config.js)
    webpack安装低于4版本(没有配置webpack.config.js)
    什么是webpack以及为什么使用它
    webpack前置知识2(JavaScript项目初始化)
  • 原文地址:https://www.cnblogs.com/margin-gu/p/5887957.html
Copyright © 2011-2022 走看看