Assembly卸载DLL有比较完美的解决方案吗

最近我使用vs2005做了一个addins在制作的过程中使用Assembly动态加载DLL,其他的地方需要实现删除DLL的操作,因此需要能够实现卸载Assembly动态加载DLL,不知道各位有没有什么好的解决方案。
大多数都说用AppDomain来实现,试验过很多,都存在这样或者那样的问题,大家有没有一个比较完美的解决方案啊。

public Assembly LoadAssembly(string filePath)
{
Assembly asm
= null;

if (this.copyToMem)
{
//先将插件拷贝到内存缓冲
byte[] addinStream = null;
if (FileHelper.ReadFileToBuff(filePath, out addinStream))
{
asm
= Assembly.Load(addinStream); //加载内存中的Dll
}
}
else
{
asm
= Assembly.LoadFrom(filePath);
}

return asm;
}

 

使用过上面的这种方式加载DLL,可是因为需要加载附加的DLL,还是失败。

红颜莎娜
浏览 385回答 1
1回答

偶然的你

请参考如下代码片段:  public DynamicAssembly()        {            PermissionSet perSet = new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted);            AppDomainSetup objSetup = new AppDomainSetup();            objSetup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;            _objAppDomain = AppDomain.CreateDomain("MyAppDomain", null, objSetup, perSet, null);        }    public void InitCall()        {            string strErrorMsg = string.Empty;            RemoteLoaderFactory factory = (RemoteLoaderFactory)_objAppDomain.CreateInstance("UIT.DynamicExpression.RemoteAccess", "UIT.DynamicExpression.RemoteAccess.RemoteLoaderFactory").Unwrap();             // with help of factory, create a real 'LiveClass' instance            _object = factory.Create("UIT.DynamicalExpressionBin.dll", "UIT.DynamicExpression.Eval", null);             if (_object == null)            {                strErrorMsg = "Error: " + "Couldn't load class.";                Trace.WriteLine(strErrorMsg);            }        } 其中RemoteFactory类代码如下:  public class RemoteLoaderFactory : MarshalByRefObject    {        private const BindingFlags bfi = BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance;         public RemoteLoaderFactory() { }         public IRemoteInterface Create(string assemblyFile, string typeName, object[] constructArgs)        {            return (IRemoteInterface)Activator.CreateInstanceFrom(                     assemblyFile, typeName, false, bfi, null, constructArgs,                     null, null, null).Unwrap();        }    }
打开App,查看更多内容
随时随地看视频慕课网APP