zoukankan      html  css  js  c++  java
  • Develope Application support plugin

    How application that support plug-in ?

    1. application knows some fixed interfaces.
    2. plug-in class must implement the fixed interface.
    3. using reflection mechanism to dynamic load plug-in.

    so, the step:
    1. define fixed interface.
    2. develope plug-in class  that implement the fixed interface.
    3. main application dynamic load the plug-in.

    e.g.

    // ---------------- Fixed plugIn Interface--------------------
    namespace PlugIn
    {
        public interface IPlugIn
        {
            void ShowInfo();
        
    }

    }
    // ------- PlugIn Module ------------
    using PlugIn;

    namespace PlugInModule
    {
        public class Class 
    : IPlugIn
        {
            #region IPlugIn Members

            void IPlugIn.ShowInfo()
            {
                MessageBox.Show("This class implement IPlugIn interface", "PlugInModule")
    ;
            
    }


            #endregion
        }
    }
    // ------------ Main Application ----------
      private void PlugIn_Click(object sender, EventArgs e)
            
    {
                OpenFileDialog dlg = new OpenFileDialog();
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    Assembly asm = Assembly.LoadFrom(dlg.FileName);
                    Type[] asmTypes = asm.GetTypes();
                    for (int i = 0; i < asmTypes.Length; i++)
                    {
                        Type interfaceType = asmTypes[i].GetInterface("IPlugIn");
                        if (interfaceType != null)
                        {
                            object objInstance = asm.CreateInstance(asmTypes[i].FullName);
                            IPlugIn IPlug = objInstance as IPlugIn;
                            if (IPlug != null)
                            {
                                listBox1.Items.Add(asmTypes[i].FullName);
                                IPlug.ShowInfo();
                            
    }

                        }
                    }
                }            
            }





    That's all!
    Fine Day!

  • 相关阅读:
    【Vijos-P1285】佳佳的魔法药水-Dijkstra思想
    【NOIP2009提高组T3】最优贸易-双向SPFA
    【NOIP2009提高组T3】最优贸易-双向SPFA
    【Vijos-P1046】观光旅游-Floyd求最小环
    【Vijos-P1046】观光旅游-Floyd求最小环
    【Vijos-P1060】盒子-DP+组合数学
    mysql 结合keepalived测试
    set global read_only=0; 关闭只读,可以读写 set global read_only=1; 开始只读模式
    set global read_only=0; 关闭只读,可以读写 set global read_only=1; 开始只读模式
    -F, --flush-logs
  • 原文地址:https://www.cnblogs.com/vsignsoft/p/839309.html
Copyright © 2011-2022 走看看