.NET 4 - 运行$ C $从串c - 在C#/ F#/ IronRuby的/ IronPython的 [英] .net 4 - run code from string - in C# / F# / IronRuby / IronPython

查看:217
本文介绍了.NET 4 - 运行$ C $从串c - 在C#/ F#/ IronRuby的/ IronPython的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

做了什么方法,我在.NET运行从字符串code?

What ways do I have in .net to run code from string?

Console.WriteLine(Compile("1 + 2 * 3"));    // results is 7.

我们还有什么其他的选择?

What other options do we have?

  • 在C# - 编译在运行时的code
  • 的IronRuby / IronPython的?
  • 在F#?

推荐答案

因此​​,动态语言,如IronRuby和IronPython的使它很容易做你想要什么。创建脚本引擎,并执行串。易作为。

So, the dynamic languages like IronRuby and IronPython make it very easy to do what you want. Create a script engine and execute the string. Easy as that.

C#,这是可能的,用code DOM ......但每一次它编译code到DLL。这是一个昂贵的行为,并可能导致您的应用程序在内存中的大小随着时间的推移,如果你不这样做了一些棘手的问题与应用程序域。这是可以做到的,但我真的preFER使用IronRuby的IronPython的或在此做法。这是更简单。

C#, it is possible, using the Code DOM... but it it compiles the code every time into a DLL. This is a costly behavior and can cause your app to grow in memory size over time if you don't do some tricky stuff with App Domains. It CAN be done, but I really prefer using IronRuby or IronPython over this approach. It is much more simple.

最后,在C#5.0中,我们期待看到编译器为托管服务。这意味着你将能够在将来运行的eval(// C#code)...但它尚未推向市场。

Finally, in C# 5.0, we are expecting to see the compiler as a managed service. This means you will be able to run eval("//c#code") in the future... but it is not available yet.

希望它能帮助!

这篇关于.NET 4 - 运行$ C $从串c - 在C#/ F#/ IronRuby的/ IronPython的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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