sharpdevelop中如何动态加载程序集

Sharpdevelop将整个程序的功能分散到了多个工程中,这样开发的时候,一个工程就不用加载过多的文件,编译的速度也加快了,IDE的反应速度也能快不少,另外程序在维护的时候,也能方便的替换相应的Assembly来实现不同的功能,组件的升级.
Sharpdevelop将动态加载的功能同样也放在了Addin类中,但是加载的Assembly放在了AddInTree中的AssemlbyHashTable,我使用了一个Dictinary的泛型来替代了原有的Hashtable
Sharpdevelop作者为了保证AddInTree的唯一性使用了一个singleton模式,
private void Initlization()
{

    System.Diagnostics.Debug.Assert(File.Exists(xmlfile), 
"配置文件不存在"
);
    XmlDocument doc 
= new
 XmlDocument();
    doc.Load(xmlfile); 
    
//加载addin的版本信息,作者信息


    
foreach (object obj in doc.DocumentElement.ChildNodes)
    
{
    
if (!(obj is
 XmlElement))
        
continue
;
    XmlElement curel 
= obj as
 XmlElement;
    
switch
 (curel.Name)
    
{
        
case "Runtime"
:
        LoadAssembly(curel);
        
break
;
        
default
:
        
break
;
    }


    
    }

}


private void LoadAssembly(XmlElement curel)
{
    
foreach (object  obj in
 curel.ChildNodes)
    
{
    
if (!(obj is
 XmlElement))
        
continue
;


//
    <Runtime>
//
        <Import assembly="cxytestdll.dll"/>        
//
    </Runtime>  
//取出cxytestdll.dll这个文本

    XmlElement xe = obj as XmlElement;
    
string asmfilename = xe.Attributes["assembly"
].InnerText;
    Debug.Assert(
!string.IsNullOrEmpty(asmfilename), "add 中runtime的结点中assembly不能为空,其中包含着assembly的路径"
);

//assebmly的路径,并加载

    string asmpath = Path.GetDirectoryName(xmlfile) + asmfilename;
    DefaultAddinTree.AddinTree.LoadAssembly(asmfilename);
    
    }

}



using System;
using
 System.Collections.Generic;
using
 System.Text;
using
 System.Windows.Forms;
using
 System.Reflection;

namespace
 CSLearn
{
    
public interface
 IAddinTree
    
{
        
void LoadAssembly(string
 assemblyfile);
    }


    
public class DefaultAddinTree:IAddinTree
    
{
        Dictionary
<string, Assembly>
 asm;
         DefaultAddinTree()
        
{
            asm 
= new Dictionary<string, Assembly>
();
        }


        
public Dictionary<string, Assembly> LoadedAssembly
        
{
            
get return asm; }

        }


       
static DefaultAddinTree thetree;
//不规范的singlton

        public static DefaultAddinTree AddinTree
        
{
            
get

            
{
                
if (thetree == null
)
                    thetree 
= new
 DefaultAddinTree();
                
return
 thetree; 
            }

        }
 

        
IAddinTree 成员

    }



}
原文地址:https://www.cnblogs.com/sunbingzibo/p/971619.html