如何在 .NET 中使用 Ruby 代码?

发布于 2024-08-05 06:16:28 字数 206 浏览 3 评论 0原文

我想在我的 C# 应用程序中使用 RubyGem。

我已经下载了 IronRuby,但我不知道如何启动和运行。他们的下载包括 ir.exe,并且还包括一些 DLL,例如 IronRuby.dll。

在我的 .NET 项目中引用 IronRuby.dll 后,如何向我的 C# 代码公开 *.rb 文件的对象和方法?

非常感谢,

迈克尔

I'd like to use a RubyGem in my C# application.

I've downloaded IronRuby, but I'm not sure how to get up and running. Their download includes ir.exe, and it includes some DLLs such as IronRuby.dll.

Once IronRuby.dll is referenced in my .NET project, how do I expose the objects and methods of an *.rb file to my C# code?

Thanks very much,

Michael

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回首观望 2024-08-12 06:16:28

这就是进行互操作的方式:

确保您有对 IronRubyIronRuby.LibrariesMicrosoft.ScriptingMicrosoft.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();


        }
    }
}

注意,运行时有一个 ExecuteFile,您可以使用它来执行您的文件。

要让 Gems 运行,

  1. 请确保使用 igem.exe 安装 gem,
  2. 您可能需要使用 Engine.SetSearchPaths 设置一些搜索路径

This is how you do interop:

Make sure you have refs to IronRuby, IronRuby.Libraries, Microsoft.Scripting and 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();


        }
    }
}

Note, Runtime has an ExecuteFile which you can use to execute your file.

To get the Gems going

  1. Make sure you install your gem using igem.exe
  2. you will probably have to set some search paths using Engine.SetSearchPaths
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文