Can I compile IronRuby project in VS2010 into DLL/exe file?

我与影子孤独终老i 提交于 2019-12-05 11:51:05
Steve

You can't compile your IronRuby classes into a .NET assembly and then access them from another assembly.

Preet is right that the nearest you can get is to embed your IronRuby scripts in (say) a C# assembly. From the C# side it would then be possible to instantiate your Ruby classes. So given the following Ruby class:

class HelloWorld
  def say_hello
    puts 'Hello'
  end
end

You could load this from a resource file and run it from 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();

There's a great IronRuby gem that packages IronRuby files into a Windows executable.

https://github.com/kumaryu/irpack

To use:

igem install irpack
irpack -o Program.exe Program.rb

Since Iron Ruby is simply DLR based code. You should be able to add the code into a resource of any assembly. Ultimately you'll need a bootstrap to load the code. That will most likely be a CLR language.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!