I want to use IronRuby as a scripting language (as Lua, for example) in my .NET project. For example, I want to be able to subscribe from a Ruby script to specific events, fired in the host application, and call Ruby methods from it.
我想在我的.NET项目中使用IronRuby作为脚本语言(例如Lua)。例如,我希望能够从Ruby脚本订阅特定事件,在宿主应用程序中触发,并从中调用Ruby方法。
I'm using this code for instantiating the IronRuby engine:
我正在使用此代码实例化IronRuby引擎:
Dim engine = Ruby.CreateEngine()
Dim source = engine.CreateScriptSourceFromFile("index.rb").Compile()
' Execute it
source.Execute()
Supposing index.rb contains:
假设index.rb包含:
subscribe("ButtonClick", handler)
def handler
puts "Hello there"
end
How do I:
我如何:
- Make C# method Subscribe (defined in host application) visible from index.rb?
- Invoke later handler method from the host application?
使用C#方法订阅(在主机应用程序中定义)可以从index.rb中看到吗?
从宿主应用程序调用以后的处理程序方法?
1 个解决方案
#1
7
You can just use .NET events and subscribe to them within your IronRuby code. For example, if you have the next event in your C# code:
您可以使用.NET事件并在IronRuby代码中订阅它们。例如,如果您的C#代码中有下一个事件:
public class Demo
{
public event EventHandler SomeEvent;
}
Then in IronRuby you can subscribe to it as follows:
然后在IronRuby中,您可以按如下方式订阅它:
d = Demo.new
d.some_event do |sender, args|
puts "Hello there"
end
To make your .NET class available within your Ruby code, use a ScriptScope
and add your class (this
) as a variable and access it from within your Ruby code:
要在Ruby代码中使用.NET类,请使用ScriptScope并将您的类(this)添加为变量并从Ruby代码中访问它:
ScriptScope scope = runtime.CreateScope();
scope.SetVariable("my_class",this);
source.Execute(scope);
And then from Ruby:
然后从Ruby:
self.my_class.some_event do |sender, args|
puts "Hello there"
end
To have the Demo class available within the Ruby code so you can initialize it (Demo.new), you need to make the assembly "discoverable" by IronRuby. If the assembly is not in the GAC then add the assembly directory to IronRuby's search paths:
要在Ruby代码中使用Demo类以便初始化它(Demo.new),您需要使IronRuby使程序集“可被发现”。如果程序集不在GAC中,则将程序集目录添加到IronRuby的搜索路径:
var searchPaths = engine.GetSearchPaths();
searchPaths.Add(@"C:\My\Assembly\Path");
engine.SetSearchPaths(searchPaths);
Then in your IronRuby code you can require the assembly, for example: require "DemoAssembly.dll"
and then just use it however you want.
然后在您的IronRuby代码中,您可以要求程序集,例如:require“DemoAssembly.dll”然后根据需要使用它。
#1
7
You can just use .NET events and subscribe to them within your IronRuby code. For example, if you have the next event in your C# code:
您可以使用.NET事件并在IronRuby代码中订阅它们。例如,如果您的C#代码中有下一个事件:
public class Demo
{
public event EventHandler SomeEvent;
}
Then in IronRuby you can subscribe to it as follows:
然后在IronRuby中,您可以按如下方式订阅它:
d = Demo.new
d.some_event do |sender, args|
puts "Hello there"
end
To make your .NET class available within your Ruby code, use a ScriptScope
and add your class (this
) as a variable and access it from within your Ruby code:
要在Ruby代码中使用.NET类,请使用ScriptScope并将您的类(this)添加为变量并从Ruby代码中访问它:
ScriptScope scope = runtime.CreateScope();
scope.SetVariable("my_class",this);
source.Execute(scope);
And then from Ruby:
然后从Ruby:
self.my_class.some_event do |sender, args|
puts "Hello there"
end
To have the Demo class available within the Ruby code so you can initialize it (Demo.new), you need to make the assembly "discoverable" by IronRuby. If the assembly is not in the GAC then add the assembly directory to IronRuby's search paths:
要在Ruby代码中使用Demo类以便初始化它(Demo.new),您需要使IronRuby使程序集“可被发现”。如果程序集不在GAC中,则将程序集目录添加到IronRuby的搜索路径:
var searchPaths = engine.GetSearchPaths();
searchPaths.Add(@"C:\My\Assembly\Path");
engine.SetSearchPaths(searchPaths);
Then in your IronRuby code you can require the assembly, for example: require "DemoAssembly.dll"
and then just use it however you want.
然后在您的IronRuby代码中,您可以要求程序集,例如:require“DemoAssembly.dll”然后根据需要使用它。