AssemblyResolve event fires when calling Assembly.Load(byte())

前端 未结 1 1282
慢半拍i
慢半拍i 2021-01-13 04:26

So I have a WPF project that is pulling in dlls that are used by another project here at my job. It\'s a mess of dependencies, I\'ve been using the technique here: http://w

1条回答
  •  被撕碎了的回忆
    2021-01-13 04:57

    Loading an assembly from byte[] is a good way to end up in .dll hell (the place you go for too many/complex dependencies). Problem here is that although you loaded the dll to an AppDomain it is not automatically resolved, when you need it again for dependent types. I commented on this problem here: AssemblyResolve Does not fire

    Long story short, Assemblies are loaded into different "contexts" inside of AppDomains. The context used by Load(byte[]) does not resolve Assemblies automatically.

    The solution is keeping track of the loaded assemblies and returning the already loaded assembly instead of loading it a second time. There is a starting point to this approach in my answer to: Need to hookup AssemblyResolve event when DisallowApplicationBaseProbing = true

    But I think you got it right with your workaround.

    BTW. Loading an assembly twice is a way to get identical but incompatible types. Ever cast an object of MyType from MyAssembly into MyType from the very same assembly and got null?

    That's a warm "Welcome to .dll hell".

    0 讨论(0)
提交回复
热议问题