C# 无法从IronRuby更新变量值
我一直在尝试将Ruby嵌入到我正在编写的C程序中。 我可以执行脚本,但在互操作方面有问题。 我似乎无法从Ruby中提取变量 我们考虑如下:C# 无法从IronRuby更新变量值,c#,ruby,scripting,ironruby,C#,Ruby,Scripting,Ironruby,我一直在尝试将Ruby嵌入到我正在编写的C程序中。 我可以执行脚本,但在互操作方面有问题。 我似乎无法从Ruby中提取变量 我们考虑如下: String before = "hello world"; String after = "changed"; ScriptRuntime mRuntime = Ruby.CreateRuntime(); ScriptEngine mEngine = mRuntime.GetEngine("ruby"); ScriptScope scope = mEngi
String before = "hello world";
String after = "changed";
ScriptRuntime mRuntime = Ruby.CreateRuntime();
ScriptEngine mEngine = mRuntime.GetEngine("ruby");
ScriptScope scope = mEngine.CreateScope();
String code = "p msg\nmsg = '" + after + "'";
ScriptSource script = mEngine.CreateScriptSourceFromString(code, SourceCodeKind.Statements);
scope.SetVariable("msg", before);
script.Execute(scope);
String result = scope.GetVariable("msg");
最初,这是使用TryGetVariable实现的
在我的单元测试中,我在result==changed上设置了一个断言,但它带有它的默认值。
我还尝试在Ruby local和$global中创建一个变量并读取它,但它似乎从未存在过
我有点卡住了,任何帮助都将被感激
String before = "hello world";
String after = "changed";
ScriptRuntime mRuntime = Ruby.CreateRuntime();
ScriptEngine mEngine = mRuntime.GetEngine("ruby");
ScriptScope scope = mEngine.CreateScope();
String code = "self.msg = '" + after + "'.to_clr_string";
ScriptSource script = mEngine.CreateScriptSourceFromString(code, SourceCodeKind.Statements);
scope.SetVariable("msg", before);
script.Execute(scope);
String result = scope.GetVariable("msg");
这对我很有用。这很有用。IronRuby可以使用更好的互操作文档。