我正在寻找的东西似乎与Roslyn编译器的脚本编译和编译功能重叠,因此我对应该使用哪一个感到困惑。
我喜欢声明/定义变量(并评估它们的值),因为我在某些点列表然后可能是名称/类型。我需要CodeAnalysis包或编译器吗?怎么样?
示例:
// I'd like to add the following to my scripting or compilation engine:
int a = 1 + 2;
string b = "some " + a;
// and then get a listing of their names, types, and values:
foreach(var myvar in script.ListVariables())
Console.WriteLine(myvar.Name + " " + myvar.Type + " " + myvar.Value.ToString());
答案 0 :(得分:0)
实际上我找到了答案,这很简单。我只需要脚本引擎。为了得到变量,我做了类似下面代码片段中的最后一行的内容:
var script = CSharpScript.Create("");
script = script.ContinueWith("int a = 10;");
script = script.ContinueWith("int b = 20;");
var vars = script.RunAsync().Result.Variables;
foreach(var v in vars)
{
Console.WriteLine($"{v.Name}, {v.Type}, {v.Value.ToString()}");
}
结果如下:
a, System.Int32, 10
b, System.Int32, 20