基于AppDomain应用程序域的“热拔插”
public class Loader : MarshalByRefObject
{
// Methods
public object LoadObject(string dllName, string typeName)
{
return Assembly.LoadFrom(dllName).CreateInstance(typeName);
}
public T GetInstance<T>(string typeName) where T : class
{
if (remoteLoader == null) return null;
return remoteLoader.GetInstance<T>(typeName);
}
public void ExecuteMothod(string typeName, string methodName)
{
remoteLoader.ExecuteMothod(typeName, methodName);
}
}
public class ForeignLoader
{
// Fields
private Hashtable htAppDomain = new Hashtable();
// Methods
public object LoadObject(string dllName, string typeName)
{
lock (this)
{
if ((dllName == null) || (typeName == null))
{
return null;
}
AppDomain domain = (AppDomain) this.htAppDomain[dllName];
if (domain == null)
{
AppDomainSetup info = new AppDomainSetup();
info.ShadowCopyFiles = "true";
domain = AppDomain.CreateDomain(dllName, null, info);
this.htAppDomain.Add(dllName, domain);
}
Loader loader = (Loader) domain.CreateInstanceFromAndUnwrap("ESBase.dll", "ESBase.Advanced.Loader");
return loader.LoadObject(dllName, typeName);
}
}
public object LoadObject(AppDomain destDomain, string dllName, string typeName)
{
if (((destDomain == null) || (dllName == null)) || (typeName == null))
{
return null;
}
Loader loader = (Loader) destDomain.CreateInstanceFromAndUnwrap("ESBase.dll", "ESBase.Advanced.Loader");
return loader.LoadObject(dllName, typeName);
}
public void Unload(string dllName)
{
lock (this)
{
AppDomain domain = (AppDomain) this.htAppDomain[dllName];
if (domain != null)
{
AppDomain.Unload(domain);
this.htAppDomain.Remove(dllName);
}
}
}
}
-------------------------AssemblyDynamicLoader其他同学的方法-------------------
public class AssemblyDynamicLoader
{
private AppDomain appDomain;
private RemoteLoader remoteLoader;
public AssemblyDynamicLoader()
{
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationName = "ApplicationLoader";
setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
setup.PrivateBinPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "private");
setup.CachePath = setup.ApplicationBase;
setup.ShadowCopyFiles = "true";
setup.ShadowCopyDirectories = setup.ApplicationBase;
AppDomain.CurrentDomain.SetShadowCopyFiles();
this.appDomain = AppDomain.CreateDomain("ApplicationLoaderDomain", null, setup);
String name = Assembly.GetExecutingAssembly().GetName().FullName;
this.remoteLoader = (RemoteLoader)this.appDomain.CreateInstanceAndUnwrap(name, typeof(RemoteLoader).FullName);
}
public void LoadAssembly(string assemblyFile)
{
remoteLoader.LoadAssembly(assemblyFile);
}
public T GetInstance<T>(string typeName) where T : class
{
if (remoteLoader == null) return null;
return remoteLoader.GetInstance<T>(typeName);
}
public void ExecuteMothod(string typeName, string methodName)
{
remoteLoader.ExecuteMothod(typeName, methodName);
}
public void Unload()
{
try
{
if (appDomain == null) return;
AppDomain.Unload(this.appDomain);
this.appDomain = null;
}
catch (CannotUnloadAppDomainException ex)
{
throw ex;
}
}
}
这样我们每次都是通过AssemblyDynamicLoader类进行跨域的访问.
AppDomain.CurrentDomain.SetShadowCopyFiles();
this.appDomain = AppDomain.CreateDomain("ApplicationLoaderDomain", null, setup);
String name = Assembly.GetExecutingAssembly().GetName().FullName;
this.remoteLoader = (RemoteLoader)this.appDomain.CreateInstanceAndUnwrap(name, typeof(RemoteLoader).FullName);
邮箱:steven9801@163.com
QQ: 48039387