在.net中加载DLL无锁定它 [英] Load dll in .net without locking it

查看:117
本文介绍了在.net中加载DLL无锁定它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作中,我要加载DLL,并从中得到一些信息,如类名和等任务......但是当我的DLL加载到我的代码,它就会被锁定,不能从建源代码,直到我关闭加载程序,我已经试过一定的解决方案,但没有人对我的作品

I Am working on a task in which i have to load dll and get some information from it like class names and etc... but when i load that dll into my code, it gets locked and can't be build from source code until i close loading program, I have tried certain solution but none of them works for me


  1. 影拷贝:在这种情况下,当我卷影副本装配在那之后,如果我有改变了结果$ b $的东西b我的主要DLL这将是仍然旧的在我的加载的应用程序。

  1. Shadowcopy: in this case when i shadow copy assembly then after that if i have changed something in
    my main dll it will be still old one in my loading application.

System.Reflection.assembly.loadfrom(System.IO.GetBytes(ASM-路径)); //但有时无法正常工作总是

System.Reflection.assembly.loadfrom(System.IO.GetBytes("asm-path")); //work sometimes but not always

System.Reflection.assembly.ReflectionOnlyConext(); //不工作

System.Reflection.assembly.ReflectionOnlyConext(); //Does not work

有没有妥善解决这个

推荐答案

一个方法是读取文件的字节数和使用的Assembly.Load的是发生在一个字节数组类似于你在你的第二个例子做什么超载。我不知道是什么 System.IO.GetBytes 终止,但尝试 File.ReadAllBytes 代替。

One way would be to read the bytes of a file and use the overload of Assembly.Load that takes in a byte array similar to what you're doing in your second example. I'm not sure what System.IO.GetBytes is but try File.ReadAllBytes instead.

byte[] assemblyBytes = File.ReadAllBytes("asm-path");
var assembly = Assembly.Load(assemblyBytes);



不过,这可能不是取决于你想做的事,因为你不能卸载的组件足够了什么之后它被装载。要解决这一点,如果你需要,你可以在程序集加载到自己的的AppDomain ,然后卸载的AppDomain一旦你完成了它

However this may not be enough depending on what you want to do since you can't unload an assembly after it's been loaded. To workaround this, if you need to, you could load the assembly into its own AppDomain and then unload the AppDomain once you've finished with it.

AppDomain ad = new AppDomain.CreateDomain("NewAssemblyAD");
byte[] assemblyBytes = File.ReadAllBytes("asm-path");
var assembly = ad.Load(assemblyBytes);  



一旦你完成与组装 ,对象,你可以卸载的AppDomain

Once you've finished with the assembly object, you can unload the AppDomain.

AppDomain.Unload(ad);

这篇关于在.net中加载DLL无锁定它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆