插件模式

 

這裏是c#的例子.運行時類型綁定.

 

ArrayList images = new ArrayList ();

//這裏的names可以從配置文件中獲取

foreach (string name in names) 

{    

    Assembly a = Assembly.Load (name);   

    Type type = a.GetType ("PlugIn");    

   MethodInfo method = type.GetMethod ("GetImage");  

  Object obj = Activator.CreateInstance (type);    

  Image image = (Image) method.Invoke (obj, null);    

  images.Add (image);}

 At the end, the ArrayList named images holds an array of Image objects representing the images obtained from the plug-ins.

發佈了142 篇原創文章 · 獲贊 3 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章