在VS2010中使用IronRuby v1.1.x创建IronRuby项目后,我几乎可以通过导入它们来使用.NET库。但实际上无法将ironruby编译成exe/DLL。 我看到了构建选项,但无法从 IronRuby 项目构建任何 exe 或 DLL。
有一个很棒的 IronRuby gem,可以将 IronRuby 文件打包到 Windows 可执行文件中。
https://github.com/kumaryu/irpack
使用方法:
igem install irpack
irpack -o Program.exe Program.rb
您无法将 IronRuby 类编译到 .NET 程序集中,然后从另一个程序集访问它们。
Preet 是对的,最接近的方法是将 IronRuby 脚本嵌入到(例如)C# 程序集中。从 C# 方面,就可以实例化您的 Ruby 类。因此给出以下 Ruby 类:
class HelloWorld
def say_hello
puts 'Hello'
end
end
您可以从资源文件加载它并从 C# 运行它:
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting.Runtime;
using IronRuby;
var runtime = IronRuby.Ruby.CreateRuntime();
var engine = runtime.GetEngine("ruby");
var assembly = Assembly.GetExecutingAssembly();
var stream = assembly.GetManifestResourceStream("PathToResource.test.rb");
string code = new StreamReader(stream).ReadToEnd();
var scope = engine.CreateScope();
engine.Execute(code, scope);
dynamic helloWorldClass = engine.Runtime.Globals.GetVariable("HelloWorld");
dynamic ironRubyObject = engine.Operations.CreateInstance(helloWorldClass);
ironRubyObject.say_hello();
因为 Iron Ruby 只是基于 DLR 的代码。您应该能够将代码添加到任何程序集的资源中。最终您需要一个引导程序来加载代码。那很可能是一种 CLR 语言。