久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

C#實現(xiàn)插件式開發(fā)的一個Demo分享

 ThinkTank_引擎 2014-10-09

winform程序,很多時候都需要用到插件式的,所以本人做了一個Demo,,思路跟網(wǎng)上思路基本一致,,現(xiàn)在共享出來如有需要的朋友可以下載,。

申明:部分代碼來源于網(wǎng)上,當(dāng)然思路也是,,呵呵


原理很簡單:


一:定義插件接口


二:實現(xiàn)插件接口并建立不同工項目,,使其在生成時生成不同的DLL


三:主程序運(yùn)行時根據(jù)接口名利用反射對插件目錄的DLL進(jìn)行加載,,加載完成后便可以使用插件接口定義的方法或?qū)傩粤恕?/p>

下面上幾張圖,,有興趣的朋友可以先看看,覺得值得一看的朋友可以下載,。


項目結(jié)構(gòu):



DefaultPlugin,PosPlugin兩個項目均為插件,,均實現(xiàn)了Iplugin接可



復(fù)制代碼

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Drawing;

using System.Windows.Forms;



namespace WinDemo.Core

{

    
public interface Iplugin

    {



        PluginInfoAttribute PluginInfo

        { 
getset; }

        
bool IsLoad

        {

            
get;

            
set;

        }



        Image ModulePicture

        {

            
get;

        }



        Image ModulePictureEnter

        {

            
get;

        }



        Image ModulePictureClick

        {

            
get;

        }



        
string ModuleName

        {

            
get;

        }





        Dictionary
<string, EventHandler> ChildNodes

        {

            
get;

        }



        ILoadForm FormLoader

        {

            
get;

            
set;

        }



       

    }

}

復(fù)制代碼


 

復(fù)制代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using System.Windows.Forms;
using System.Collections;

namespace WinDemo.Core
{
    
public static class PluginLoader
    {

        
public static List<Iplugin> plugins = new List<Iplugin>();
        
private static ArrayList piProperties = new ArrayList();
        
        
private static bool IsValidPlugin(Type t)
        {
            
bool ret = false;
            Type[] interfaces 
= t.GetInterfaces();
            
foreach( Type theInterface in interfaces ) {
                
if (theInterface.FullName == "WinDemo.Core.Iplugin")
                {
                    ret 
= true;
                    
break;
                }
            }
            
return ret;
        }


       

        
public static void LoadAllPlugins()
        {
            
string[] files = Directory.GetFiles(Application.StartupPath + "\\plugin\\");
            
int i = 0;
            PluginInfoAttribute typeAttribute 
= new PluginInfoAttribute();
            
foreach (string file in files)
            {
                
string ext = file.Substring(file.LastIndexOf("."));
                
if (ext != ".dll"continue;
                
try
                {
                    Assembly tmp 
= Assembly.LoadFile(file);
                    Type[] types 
= tmp.GetTypes();
                    
bool ok = false;
                    
foreach (Type t in types)
                        
if (IsValidPlugin(t))
                        {
                            Iplugin plugin 
= (Iplugin)tmp.CreateInstance(t.FullName);
                            plugins.Add(plugin);
                            
object[] attbs = t.GetCustomAttributes(typeAttribute.GetType(), false);
                            PluginInfoAttribute attribute 
= null;
                            
foreach (object attb in attbs)
                            {
                                
if (attb is PluginInfoAttribute)
                                {
                                    attribute 
= (PluginInfoAttribute)attb;
                                    attribute.Index 
= i;
                                    i
++;
                                    ok 
= true;
                                    
break;
                                }
                            }

                            
if (attribute != null)
                            {
                                piProperties.Add(attribute);
                                plugin.PluginInfo 
= attribute;
                            }
                            
else throw new Exception("未定義插件屬性");
                            
if (ok) break;
                        }
                }
                
catch (Exception err)
                {
                    
throw err;
                }
            }
            plugins.Sort((p1, p2) 
=> {
                
return p2.PluginInfo.Index - p1.PluginInfo.Index;
            });
        }
    }
}
復(fù)制代碼

  


復(fù)制代碼

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Drawing;

using WinDemo.Core;

using System.Windows.Forms;

 



namespace DefaultPlugin

{

    [PluginInfo(
"Default","1.0","XH","www.cnporter.com",true,2)]

    
public class Default : WinDemo.Core.Iplugin

    {

        
private Dictionary<string, EventHandler> _ChildNodes = new Dictionary<string, EventHandler>();

        
private LeftNav frmLeftNav = new LeftNav(); 

        
public static ILoadForm Formloader ;

        
public Default()

        {



            _ChildNodes.Add(
"菜單五", (sender,e) =>

            {

                MessageBox.Show(sender.ToString());

            });

            _ChildNodes.Add(
"菜單四", (sender, e) =>

            {

                MessageBox.Show(sender.ToString());

            });

            _ChildNodes.Add(
"菜單三", (sender, e) =>

            {

                MessageBox.Show(sender.ToString());

            });

            _ChildNodes.Add(
"菜單二", (sender, e) =>

            {

                MessageBox.Show(sender.ToString());

            });

            _ChildNodes.Add(
"菜單一", (sender, e) =>

            {

                FormLoader.LoadNavFrm(frmLeftNav);

            });

        }



        



        
public Image ModulePicture

        {

            
get

            {

                
return ((System.Drawing.Image)(ImageResource.Index));

            }

        }



        
public Image ModulePictureEnter

        {

            
get

            {

                
return ((System.Drawing.Image)(ImageResource.IndexEnter));

            }

        }



        
public Image ModulePictureClick

        {

            
get

            {

                
return ((System.Drawing.Image)(ImageResource.IndexClick));

            }

        }



        
public string ModuleName

        {

            
get

            {

                
return "首頁";

            }

        }



        
public Dictionary<string, EventHandler> ChildNodes

        {

            
get

            {

                
return _ChildNodes;

            }

        }









        
public bool IsLoad

        {

            
get;

            
set;

        }

        

        
public ILoadForm FormLoader

        {

            
get

            {

                
return Formloader;

            }

            
set

            {

                Formloader 
= value;

            }

        }







        
public PluginInfoAttribute PluginInfo

        {

            
get;

            
set;

        }

    }



 

}

 

復(fù)制代碼


此圖現(xiàn)在有兩個插件


 


運(yùn)行效果如下


 


源碼下載


 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購買等信息,,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報,。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多