宿主實現
publicclassHost:IHost
{
privateList<ILog>plugins=newList<ILog>();
#regionIHost成員
publicList<ILog>Plugins
{
get{returnplugins;}
}
publicintLoadPlugins(stringpath)
{
string[]assemblyFiles=Directory.GetFiles(path,"*.dll");
foreach(varfileinassemblyFiles)
{
Assemblyassembly=Assembly.LoadFrom(file);
foreach(vartypeinassembly.GetExportedTypes())
{
if(type.IsClass&&typeof(ILog).IsAssignableFrom(type))
{
ILogplugin=Activator.CreateInstance(type)asILog;
plugins.Add(plugin);
}
}
}
returnplugins.Count;
}
publicILogGetLog(stringname)
{
foreach(variteminplugins)
{
if(item.GetType().ToString()==name)
{
returnitem;
}
}
returnnull;
}
#endregion
}
ILog的實現和上例基本一樣,請參考
主程序代碼
staticvoidMain(string[]args)
{
Host.Hosthost=newHost.Host();
host.LoadPlugins(".");
InterfaceLayer.ILoglog=host.GetLog(ConfigurationManager.APPSettings["LogType"].ToString());
log.Write(newException("異常測試"));
}