本文实例讲述了C#实现动态加载dll的方法。分享给大家供大家参考。具体实现方法如下:
namespace Alif.CommonAPI.DynamicLoadAssembly
{
public class AssemblyDynamicLoader<T>
{
private AppDomain appDomain;
private DynamicRemoteLoadAssembly<T> remoteLoader;
public T InvokeMethod(string assemblyName, string assemblyPath, string assemblyConfigFilePath, string fullClassName, string methodName, params object[] args)
{
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationName = \"ApplicationLoader\";
setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory + @\"bin\\\";
//setup.PrivateBinPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, \"private\");
setup.CachePath = setup.ApplicationBase;
setup.ShadowCopyFiles = \"true\";
if (assemblyConfigFilePath != string.Empty)
{
setup.ConfigurationFile = AppDomain.CurrentDomain.BaseDirectory + assemblyConfigFilePath;
}
setup.ShadowCopyDirectories = setup.ApplicationBase;
setup.LoaderOptimization = LoaderOptimization.SingleDomain;
this.appDomain = AppDomain.CreateDomain(\"ApplicationLoaderDomain\", null, setup);
String name = Assembly.GetExecutingAssembly().GetName().FullName;
this.remoteLoader = (DynamicRemoteLoadAssembly<T>)this.appDomain.CreateInstanceAndUnwrap(name, typeof(DynamicRemoteLoadAssembly<T>).FullName);
assemblyName = AppDomain.CurrentDomain.BaseDirectory + assemblyPath + assemblyName;
return this.remoteLoader.InvokeMethod(assemblyName, fullClassName, methodName, args);
}
/// <summary>
///
/// </summary>
public void Unload()
{
try
{
AppDomain.Unload(this.appDomain);
this.appDomain = null;
}
catch (CannotUnloadAppDomainException ex)
{
}
}
}
}
本文地址:https://www.stayed.cn/item/22785
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我