📜  如何在 c# 代码示例中评估代码

📅  最后修改于: 2022-03-11 14:49:18.679000             🧑  作者: Mango

代码示例1
//Unfortanatly you cant because c# isnt a Dynamic language
//but you can use the Roslyn scripting API
// add NuGet package 'Microsoft.CodeAnalysis.Scripting'
using Microsoft.CodeAnalysis.CSharp.Scripting;

await CSharpScript.EvaluateAsync("System.Math.Pow(2, 4)") // returns 16
//You can also run any piece of code:
var script = await CSharpScript.RunAsync(@"
               class MyClass
               { 
                   public void Print() => System.Console.WriteLine(1);
               }");
//And reference the code that was generated in previous runs:
await script.ContinueWithAsync("new MyClass().Print();");