【问题标题】:Can I compile IronRuby project in VS2010 into DLL/exe file?我可以将 VS2010 中的 IronRuby 项目编译成 DLL/exe 文件吗?
【发布时间】:2010-11-16 04:02:28
【问题描述】:

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

【问题讨论】:

  • stackoverflow.com/questions/561509/… 的副本。 (不是严格重复,但答案是一样的。)
  • 但是仍然不知道如何将它编译成 .NET 程序集?而这一次是在 IronRuby 而不是 IronPython。
  • 那是因为它本身是不可能的。没关系;您不能这样做,因为 IronRuby 和 IronPython 都使用 DLR。实际的语言根本不重要。

标签: compiler-construction ironruby


【解决方案1】:

有一个很棒的 IronRuby gem 可以将 IronRuby 文件打包成一个 Windows 可执行文件。

https://github.com/kumaryu/irpack

使用方法:

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

【讨论】:

    【解决方案2】:

    您不能将 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();
    

    【讨论】:

    • 如果 Visual Studio 可以将 IronRuby 代码编译成 EXE 就好了。
    【解决方案3】:

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

    【讨论】:

    • 这是否意味着我必须获取 IronRuby 源代码并将其与我的 Ruby 项目链接并使用 .NET 进行编译?
    猜你喜欢
    • 2016-06-05
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-06
    • 2017-08-19
    相关资源
    最近更新 更多