第一次执行代码后动态执行失败? [英] Executing code dynamically failed after the first time?

查看:73
本文介绍了第一次执行代码后动态执行失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚学习了一些代码,可以使用CodeDom动态执行csharp代码的输入字符串,我已经做过,并且只有第一次才能使用,如果我想输入新的代码字符串,我必须重新启动我的应用程序.我知道临时dll仍在使用中,使用时无法重建.您能给我一个解决方案吗?有人说我们必须卸载临时程序集并建议一些代码片段,但是我尝试了,但又有一个例外,我认为这对我来说有点棘手,请帮帮我.如果可能的话,请给我所有解决问题的方法,即使是名称或关键字也可以,当然请至少提供一个详细信息.
您的帮助将不胜感激!
谢谢!

Hi, I have just learnt some code to execute dynamically an input string of csharp code using CodeDom, I''ve done and can enjoy it only the first time, if I want to input a new code string, I have to restart my application. I know the temporary dll was still in use and it couldn''t be re-built when it was in use. Could you please give me a solution for this? Someone says we have to unload the temporary assembly and suggests some snippet of code but I tried and there was another exception, I think this is a little tricky to me, please help me out. If possible, give me all the ways can solve my problem, even a name or some key word is fine, of course make details on at least a one please.
Your help would be appreciated!
Thanks!

推荐答案

当然.这实际上是一个很大的问题.并且唯一正确的解决方案是编译并使用在单独的应用程序域中动态创建的程序集.

原因如下:.NET的体系结构不允许卸载程序集.这是一种安全措施:很难保证在卸载程序集后没有代码在使用它.如果程序集加载到单独的应用程序域"中,这将不成问题,因为它们是完全隔离的,并且是独立的进程.因此,唯一的方法是使用单独的应用程序域,您可以在其中加载所有程序集的同时卸载该应用程序域.这种方法将完全解决您的问题,但会带来一个复杂的问题:您将需要遍历Application Domains之间的隔离墙.由于它们隔离得很好,因此只能通过IPC(进程间通信)来完成.

System.AppDomain类简化了IPC设施来解决此问题.

请参阅:
http://msdn.microsoft.com/en-us/library/system.appdomain%28v = vs.100%29.aspx [创建使用可重载插件的WPF应用程序... [^ ],
AppDomain拒绝加载程序集 [在现有实例上的C#反射InvokeMember [项目和DLL:如何使其可管理? [ ^ ].

这些答案是关于将应用程序域与代码DOM一起使用:
使用CodeDom生成代码 [创建使用可重载插件的WPF应用程序... [ ^ ].

—SA
Of course. This is actually a considerable problem; and its only correct resolution is compiling and using your assembly created on the fly in a separate Application Domain.

Here is why: the architecture of .NET does not allow unloading of an assembly. This is done as a safety measure: it''s hard to guarantee that after unloading of an assembly no code is using it. If the assembly is loaded in a separate Application Domain, this is not a problem, because they are thoroughly isolated, as well as separate processes. Therefore, the only way is to work with a separate Application Domain which you can unload with all the assemblies loaded in it. This approach will completely resolve your problem but with one complication: you will need to work through the wall between Application Domains. As they are well isolated, this can be done through IPC (Inter-Process Communication) only.

The class System.AppDomain has simplified IPC facilities to solve this problem.

Please see:
http://msdn.microsoft.com/en-us/library/system.appdomain%28v=vs.100%29.aspx[^].

Please see my past answers on related topics:
Create WPF Application that uses Reloadable Plugins...[^],
AppDomain refuses to load an assembly[^],
C# Reflection InvokeMember on existing instance[^],
Projects and DLL''s: How to keep them managable?[^].

These answers are about using Application Domains with Code DOM:
code generating using CodeDom[^],
Create WPF Application that uses Reloadable Plugins...[^].

—SA


这篇关于第一次执行代码后动态执行失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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