如何引用从 IronRuby 添加到 ScriptScope 的变量?
我正在尝试将变量添加到作用域,然后从我的 ruby 表达式访问该变量。
时间:2019-03-17 标签:c#
ScriptRuntime runtime = IronRuby.Ruby.CreateRuntime();
var scope = runtime.ExecuteFile("C:\\codebase\\Test\\Test2\\Test2\\person2.rb");
scope.SetVariable("name", "Scott");
dynamic person = scope.Engine.Execute("p = Person.new(name)");
person.sayHi();
Ruby
class Person
def initialize(name)
@name = name.capitalize
end
def sayHi
puts "Hello #{@name}!"
end
end
I'm trying to add a Variable to the scope and then access that variable from my ruby expression.
c#
ScriptRuntime runtime = IronRuby.Ruby.CreateRuntime();
var scope = runtime.ExecuteFile("C:\\codebase\\Test\\Test2\\Test2\\person2.rb");
scope.SetVariable("name", "Scott");
dynamic person = scope.Engine.Execute("p = Person.new(name)");
person.sayHi();
Ruby
class Person
def initialize(name)
@name = name.capitalize
end
def sayHi
puts "Hello #{@name}!"
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你们非常接近。只需将 C# 代码的最后两行更改为下一行:
此外,虽然我不确定您要实现什么目标,但您可以以更简单的方式使用 C# 中的类:
You were pretty close. Just change the last two lines of your C# code to the next ones:
In addition, though I'm not sure what you're trying to achieve, you can use your class from C# in an easier fashion: