Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法从IronRuby更新变量值_C#_Ruby_Scripting_Ironruby - Fatal编程技术网

C# 无法从IronRuby更新变量值

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

我一直在尝试将Ruby嵌入到我正在编写的C程序中。 我可以执行脚本,但在互操作方面有问题。 我似乎无法从Ruby中提取变量

我们考虑如下:

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可以使用更好的互操作文档。