2010-11-16 52 views
4

通过使用IronRuby v1.1.x在VS2010中创建IronRuby项目后,我通过导入它们几乎可以使用.NET库。但实际上不能将ironruby编译成exe/DLL。 我看到构建选项,但不能从IronRuby项目构建任何exe或DLL。请帮忙 !我可以将VS2010中的IronRuby项目编译成DLL/exe文件吗?

+0

http://stackoverflow.com/questions/561509/how-do-i-create-a-net-assembly-in-ironpython-的重复并从c调用它。 (不是一个严格的副本,但答案是一样的。) – cdhowie 2010-11-16 04:12:43

+0

但如何编译成.NET程序集仍然没有线索?而这次是IronRuby而不是IronPython。 – 2010-11-16 04:22:20

+0

这是因为它本身不可能。并没有关系;你不能这样做,因为IronRuby和IronPython都使用DLR。实际的语言根本无关紧要。 – cdhowie 2010-11-16 06:10:10

回答

4

您无法将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(); 
+1

如果Visual Studio可以将IronRuby代码编译为EXE,那将会很棒。 – 2011-10-07 22:32:51

0

由于Iron Ruby仅仅是基于DLR的代码。您应该能够将代码添加到任何程序集的资源中。最终你需要一个引导来加载代码。这很可能是CLR语言。

+0

这是否意味着我必须获取IronRuby源代码并将其与我的Ruby项目链接并使用.NET进行编译? – 2010-11-16 04:19:03

5

有一个伟大的IronRuby的宝石,包IronRuby的文件到一个Windows可执行文件。

https://github.com/kumaryu/irpack

要使用:

igem install irpack 
irpack -o Program.exe Program.rb 
相关问题