动态加载和卸载使用CSharpCodeProvider生成的dll [英] Dynamically loading and unloading a a dll generated using CSharpCodeProvider

查看:156
本文介绍了动态加载和卸载使用CSharpCodeProvider生成的dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,该应用程序使用一些代码脚本在运行时生成dll并根据需要调用它们,并且在我继续对其进行编码之前遇到了一些问题!

I have an app that uses some code scripts to generate dll's at run-time and invoke them as needed and have run into some questions before i go ahead and code it !

是否可以在不需要时从内存中卸载它们?
如果不是,将它们加载到单独的appdomain中并使用一些ipc等调用调用会对性能产生影响。-btw,这需要高性能/实时

Is it possible to unload them from memory when not needed ? If not - what would be the performance impact of loading them into separate appdomains and invoking the calls using some ipc etc. - btw, this needs to be highly performant/real-time

或者,如何加载生成的程序集的多个实例(当然具有不同的版本号)呢?我假设如果您释放所有未使用的引用,则gc / framework可能会将其卸载?

Alternatively, how about loading multiple instances of the generated assembly ( with different version #'s of course ) ? I'd assume if you release all references not being used, it might be unloaded by the gc/framework ?

还有一个问-会有任何冲突(不知道这是否合适吗?)在这种情况下?

Also a Q - would there be any "collisions" ( not sure if this is the right word ? ) in this scenario ?

推荐答案

只能将整个AppDomain卸载程序集。根据需要加载的动态程序集的数量,可以将它们加载到默认的AppDomain中,并让它们保留在内存中,直到关闭应用程序为止。但是,如果您认为存储空间过多并且会成为内存消耗问题,则需要将它们加载到单独的AppDomain中,使用AppDomain.DoCallBack整理跨越AppDomain边界的调用并定期卸载AppDomain。例如,Visual Studio中的 T4 主机会在一个单独的AppDomain,最多可进行25个代码生成会话,然后将其卸载以回收内存。

Assemblies can only be unloaded with the entire AppDomain. Depending on how many of the dynamic assemblies you need to load, it may be OK to load them in the default AppDomain and let them stay in memory until the application is closed. However, if you think you will have too many and the memory consumption will become a problem, you will need to load them in a separate AppDomain, use AppDomain.DoCallBack to marshall calls accross AppDomain boundaries and unload the AppDomain periodically. For example, T4 host in Visual Studio loads compiled assemblies in a separate AppDomain for up to 25 code generation sessions and then unloads the AppDomain to recycle memory.

与动态编译和生成程序集的影响相比,使用单独的AppDomain进行的额外编组的性能影响将微不足道。仅当跨域调用的数量很大时,这才成为问题。您可能需要测试AppDomain.DoCallBack的性能,以确定性能是否适合您的方案。

Performance impact of additional marshalling that needs to occur with a separate AppDomain will be insignificant compared to the impact of compiling and generating an assembly on the fly. It will only become an issue if the number of cross-domain calls is large. You will probably need to test performance of AppDomain.DoCallBack to determine if performance will be acceptable for your scenario.

这篇关于动态加载和卸载使用CSharpCodeProvider生成的dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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