首页 新闻 赞助 找找看

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

0
悬赏园豆:100 [已解决问题] 解决于 2010-02-10 15:13

最近我使用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,还是失败。

棋圣的主页 棋圣 | 初学一级 | 园豆:197
提问于:2010-02-10 12:26
< >
分享
最佳答案
0

请参考如下代码片段:

 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();
        }
    }

收获园豆:100
查尔斯 | 老鸟四级 |园豆:3832 | 2010-02-10 12:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册