简体   繁体   English

具有不同程序集引用的Assembly.CreateInstance

[英]Assembly.CreateInstance with different assembly references

I've created a plugin architecture that supports reloading of the plugins and doesn't use multiple AppDomains. 我创建了一个插件架构,该架构支持插件的重新加载并且不使用多个AppDomain。 I simply create a new assembly, when the dll changes, with the Assembly.Load(Byte[]) method and then create the plugin instance via myAssembly.CreateInstance(type.FullName) . 我只是简单地使用Assembly.Load(Byte[])方法创建一个新的程序集(当dll更改时Assembly.Load(Byte[]) ,然后通过myAssembly.CreateInstance(type.FullName)创建插件实例。 It all works fine and well. 一切正常。 Now I wanted to support that the plugin can have referenced assembies and those references can be changed on the fly. 现在,我想支持该插件可以引用组件,并且这些引用可以随时更改。 I've implemented Appdomain.AssemblyResolve event and it works, of course, to load the references of the plugin once but not to reload the referenced dlls when they change. 我已经实现了Appdomain.AssemblyResolve事件,它当然可以加载一次插件的引用,但在它们更改时不重新加载被引用的dll。

Is there any way of creating an instance from an assembly and telling it which other assemblies to use as a reference? 有什么方法可以从程序集中创建实例,并告诉它使用哪个其他程序集作为引用?

Small note: I know about app domains but cannot use them because I don't want to serialize the data I have in memory (many GBs). 小提示:我了解应用程序域,但不能使用它们,因为我不想序列化内存中的数据(许多GB)。

.NET will find your references according to the qualified assembly name and AssemblyResolve will only fire if .NET cannot already resolve the assembly it is looking for automatically (by matching the qualified name and load context to a loaded assembly, or loading from the probing path). .NET将根据合格的程序集名称查找您的引用,并且仅当.NET无法自动解析其正在查找的程序集时(通过将合格名称和装入上下文与装入的程序集匹配,或者从探测路径进行装入),AssemblyResolve才会启动)。

So if you have already resolved a particular referenced type, or it is resolved automatically, you won't get an opportunity to load it from somewhere else. 因此,如果您已经解析了特定的引用类型,或者它已自动解析,则您将没有机会从其他位置加载它。 Also, because assemblies (identified by the full assembly name) loaded in a specific context in a given AppDomain cannot be unloaded, you are not going to be able to unload/reload those referenced assemblies in the same AppDomain. 另外,由于无法卸载在给定AppDomain中的特定上下文中加载的程序集(由完整程序集名称标识),因此您将无法卸载/重新加载同一AppDomain中的那些引用程序集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM