我想在我的C#应用程序中使用RubyGem。
我已经下载了IronRuby,但不确定如何启动和运行。他们的下载文件包含ir.exe,并且包含一些DLL,例如IronRuby.dll。
在.NET项目中引用IronRuby.dll之后,如何将* .rb文件的对象和方法暴露给C#代码?
非常感谢,
麦可
这是您进行互操作的方式:
请确保您有裁判来IronRuby,IronRuby.Libraries,Microsoft.Scripting和Microsoft.Scripting.Core
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronRuby;
using IronRuby.Builtins;
using IronRuby.Runtime;
namespace ConsoleApplication7 {
class Program {
static void Main(string[] args) {
var runtime = Ruby.CreateRuntime();
var engine = runtime.GetRubyEngine();
engine.Execute("def hello; puts 'hello world'; end");
string s = engine.Execute("hello") as string;
Console.WriteLine(s);
// outputs "hello world"
engine.Execute("class Foo; def bar; puts 'hello from bar'; end; end");
object o = engine.Execute("Foo.new");
var operations = engine.CreateOperations();
string s2 = operations.InvokeMember(o, "bar") as string;
Console.WriteLine(s2);
// outputs "hello from bar"
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud)
注意,运行时具有ExecuteFile,可用于执行文件。
使宝石前进
igem.exe