在运行时编译代码 [英] Compile code in runtime

查看:105
本文介绍了在运行时编译代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!我想从运行时编写的代码创建模块,即例如,程序要求用户编写几个参数的函数,并且由于文本是属于该程序本身的模块的一部分,因此该程序对文本进行评估.换句话说,我想在运行时编译代码.我怎样才能做到这一点?谢谢.

Hello! I want to create a module from a code written during runtime, i. e., the program asks the user to write a function of several parameters and the program evaluate the text as it was part of a module which belongs to the program itself. In other words, I want to compile code during runtime. How can I do this? Thanks.

推荐答案

这并不是很难做到的.这是使用CodeDOM完成的.但是,问题是:您要使用此代码做什么?成功构建后,可以立即将代码加载到内存中.如果要在同一过程中运行它,则可以通过反射来完成.您需要实现一些插件体系结构,该体系结构将加载新创建的程序集,找到某种表示插件入口点的类型,检查其是否实现了所需的接口并创建实现类型的实例,或者只是运行一些静态方法(我不推荐).

请从这里开始:
http://msdn.microsoft.com/en-us/library/y2k85ax6%28v = vs.100%29.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/saf5ce06.aspx [ ^ ].

即使这不是最大的问题.如果要卸载此已编译代码,可能会遇到最大的问题.这可能是一个重要的要求,因为通常您需要编译并运行已编译的代码,然后再运行一次.如果是这种情况,那么麻烦是这样的:.NET不允许从进程中卸载任何已加载的程序集;这将是非常不安全的(因为潜在的某些现有代码可能会尝试调用某些已卸载的代码).由于这是不可能的,因此唯一可行的解​​决方法是在单独的应用程序域"中编译和加载已编译的程序集,您始终可以卸载并创建一个新的程序集.这里的问题是使用原始应用程序域中的主机代码进行工作:您将不得不穿越两个不同应用程序域之间的边界.由于不同的应用程序域相互隔离以及不同的进程,因此它们只能通过IPC进行通信.幸运的是,在System.AppDomain类中实现了一种轻型的IPC,例如GetDataSetData.请参阅:
http://msdn.microsoft.com/en-us/library/system.appdomain.aspx [ ^ ].

有关更多详细信息,请参阅我过去有关相关主题的答案:

插件架构和调用:
在现有实例上的C#反射InvokeMember [创建使用可重载插件的WPF应用程序... [^ ],
AppDomain拒绝加载程序集 [项目和DLL:如何使其可管理? [ ^ ].

将插件体系结构与CodeDOM结合使用:
使用CodeDom生成代码 [动态加载用户控件 [
This is not very hard to do. This is done using CodeDOM. The problem however is: what do you want to do with this code? The code could be loaded in memory right after successful build. If you want to run it in the same process, this can be done via Reflection. You need to implement some plug-in architecture which would load the newly created assembly, locate some type which would represent plug-in entry point, check if it implements required interface and create an instance of the implementing type, or just run some static method (which I would not recommend).

Please start here:
http://msdn.microsoft.com/en-us/library/y2k85ax6%28v=vs.100%29.aspx[^],
http://msdn.microsoft.com/en-us/library/saf5ce06.aspx[^].

Even this is not a biggest problem. The biggest problem you may face if you want to have this compiled code unloadable. This could be an important requirement, because usually you would need to compile and run the compiled code more then once. If this is the case, the complication is this: .NET does not allowed unloading of any loaded assembly from the process; it would be very unsafe (because potentially some existing code could try to call some code which was unloaded). As this is impossible, the only available work around is compiling and loading a compiled assembly in a separate Application Domain, which you can always unload and create a new one. The problem here is working with you host code in the original Application Domain: you will have to work through the boundary between two different Application Domain. As different Application Domains are isolated from each other as well as different processes, they can only communicate through IPC. Fortunately, there is a light form of IPC implemented in System.AppDomain class, such as GetData and SetData. Please see:
http://msdn.microsoft.com/en-us/library/system.appdomain.aspx[^].

For further detail, please see my past answers on related topics:

Plug-in architecture and invocation:
C# Reflection InvokeMember on existing instance[^],
Create WPF Application that uses Reloadable Plugins...[^],
AppDomain refuses to load an assembly[^].

Effect of plug-in architecture on dependency layers:
Projects and DLL''s: How to keep them managable?[^].

Using plug-in architecture with CodeDOM:
code generating using CodeDom[^].

Simple part only: working without application domain when re-loading of assemblies is not required:
Dynamically Load User Controls[^].

—SA


这篇关于在运行时编译代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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