Load same dll multiple times

瘦欲@ 提交于 2021-02-10 05:57:09

问题


I’m trying to load different versions of my dll while my host program is running. I can’t use different AppDomains and unload them, the reason is that my host program is just a test-setup and the real application (Autodesk 3ds Max) doesn’t support it and I can’t change anything about that. This is how I’m loading my dll in my test program:

var testAssembly = Assembly.LoadFrom(textBox1.Text);
var formObject = testAssembly.CreateInstance("Dll_Test.Form1");
(formObject as Form).Show();

I read here: https://msdn.microsoft.com/en-us/library/dd153782.aspx#load_contexts “If an assembly with the same identity is already loaded, LoadFrom returns the loaded assembly even if a different path was specified.”

So this is exactly my problem, at first I simply tried to rename my dll, compile my new one with some changes and call LoadFrom again. However, this didn’t show my changes and I assume this is because the assembly still has the same identity as the previous one. I thought I could change the identity by signing the assembly (https://msdn.microsoft.com/en-us/library/wd40t7ad.aspx). Unfortunately the third party dll I have to work with is not signed.

Is there a way to change the identity of my dll every time I compile without changing the Assembly name? This way I could keep using the same path string in Assembly.LoadFrom(path). If not I can work around the Assembly name change but some people had doubts about changing the Assembly name on every build (Unique Assembly name every build)


回答1:


No, identity of non-strongly-signed assembly is its name alone so you have to change the name.

Note that you can use other "Load" methods and even load from byte array, but generally it will bring you more pain than worth. Blog posts by Suzanne Cook is almost required reading when you deal with assembly identity, LoadFrom and related topics.

If reloading assembly is required one solution would be to build proxy that will forward calls to other AppDomain and load you ever-changing assembly into new AppDomains (assuming you can load fully trusted code into Autodesk 3ds Max so you can manage AppDomains yourself).



来源:https://stackoverflow.com/questions/30113525/load-same-dll-multiple-times

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!