开发者

code snippets interpretation

开发者 https://www.devze.com 2023-03-28 13:02 出处:网络
Let\'s say I have a WinForm App...written in C#. Is it possib开发者_如何学编程le?After all, put my eye on Iron Python.C# is not interpreted, so unlike javascript or other interpreted languages you ca

Let's say I have a WinForm App...written in C#.

Is it possib开发者_如何学编程le?


After all, put my eye on Iron Python.


C# is not interpreted, so unlike javascript or other interpreted languages you can't do that natively. You can go four basic routes, listed here in order of least to most complex...

1) Provide a fixed set of operations that the user can apply. Parse the user's input, or provide checkboxes or other UI elements to indicate that a given operation should be applied.

2) Provide a plugin-based or otherwise dynamically defined set of operations. Like #1, this has the advantage of not needing special permissions like full trust. MEF might come in handy for this approach: http://mef.codeplex.com/

3) Use a dynamic c# compilation framework like paxScript: http://eco148-88394.innterhost.net/paxscriptnet/. This would, in theory, allow you to compile small c# snippets on demand.

4) Use IL Emit statements to parse code and generate your operations on the fly. This is by far the most complex solution, likely requires full trust, and is extremely error prone. I don't recommend it unless you have some very obscure requirements and sophisticated users.


The CSharpCodeProvider class will do what you want. For a (VERY outdated, but still working with a few tweaks) example of its use, check out CSI.


If you are willing to consider targeting the Mono runtime, the type Mono.CSharp.Evaluator provides an API for evaluating C# expressions and statements at runtime.

0

精彩评论

暂无评论...
验证码 换一张
取 消