C# 外掛程式第一步 簡介

來源:互聯網
上載者:User

有利於工程的同步開發等等....

 

接上次介紹的的反射機制

1. 外掛程式類, 建立號dll類檔案, 完成相應的功能.例如簡單的建立一個myPluginII的類庫

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace myPluginII{    public class myPluginII    {        public void PopOut(){            frmPop pop = new frmPop();            frmPop.Show(); //在類庫下添加一個視窗類別.        }    }}
build好這個類. 就會產生一個dll檔案.
 
2. 在主程式中的引用中要添加上 myPluginII
 
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;namespace myFisrtPlugin{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {            try            {                string path = Application.StartupPath ;                //path  = System.IO.Path.Combine(path,"Plugins") ;       //其實當添加好引用後,編譯主程式時,會在StartupPath 目錄下自動產生對應引用類的dll文檔.
                //當然為了清晰起見, 可以再debug目錄下建立一個Plugins的檔案夾
                 foreach ( string file in System.IO.Directory.GetFiles(path,"*.dll"))                {                    this.cmbPlugins.Items.Add(file) ;// 將dll形式的類庫放到comobox中..                }            }            catch (System.Exception ex)            {                MessageBox.Show(ex.Message);            }        }        private void button1_Click(object sender, 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)中,提取類,並把此類執行個體化
                    myPluginII.myPluginII p2 = (myPluginII.myPluginII)                         System.Activator.CreateInstance(                      asm.GetType(asmName + "." + asmName)                      );                                       //4. 在主程式中使用這個類的執行個體
                      p2.PopOut();                                                                        }             }            catch ( Exception ex )            {                MessageBox.Show(ex.Message ) ;            }        }    }}
執行個體化的時候, 當然可以寫一個借口類, 之下用多種形式的plugin來再實現.. 例如有
 
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace PluginLib{    public interface Plugin    {        string Show();        void Run();
        方法1....
        方法n    }}
新的外掛程式從這個借口整合,再重新實現之..
這樣在執行個體化的時候只要: 
                     PluginLib.Plugin tmpPlug = (PluginLib.Plugin)                          System.Activator.CreateInstance(                          asm.GetType(asmName+"."+asmName)                          );
這裡用父類指向子類,來實現多態..

asm.GetType(asmName+"."+asmName) //為該類的命名空間+類名.

另外需要在子外掛程式類中添加 PluginLib的引用.  主程式中需要添加子外掛程式類的引用.

編譯之.

然後就好用了..

 

點擊button彈出..

 

當然這個只是最基本的東西, 有很多東西仍然需要考慮.

剛接觸, 所以難免有錯誤和紕漏, 有高手不吝賜教, 請施惠..

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.