2015-10-25 27 views

回答

1

编译并在运行时执行代码始终是一个挑战。 您提到的图书馆只是一种方式。

您可以使用由Microsoft和C#团队随C#6.0和Visual Studio 2015提供的Roslyn。你无法想象它有多强大。下面是一些示例和演练:

https://github.com/dotnet/roslyn/wiki/Samples-and-Walkthroughs

和其他一些介绍:

https://en.wikipedia.org/wiki/.NET_Compiler_Platform

,这里是一些样本来创建一个REPL(像你想要的):

http://www.jayway.com/2015/05/09/using-roslyn-to-build-a-simple-c-interactive-script-engine/

使用Roslyn可以简单地有一些hing是这样的:

var csScript = 
    string.Format(@" 
     var x = Math.Max(Math.Avg({0},3),Math.Avg(low1:3)); 
     x; 
    ", high1, low1); 
    //And this from the REPL 
    Console.WriteLine(CSharpScriptEngine.Execute(csScript));