5

C# アプリケーションで RubyGem を使用したいと考えています。

IronRuby をダウンロードしましたが、起動して実行する方法がわかりません。ダウンロードには ir.exe が含まれており、IronRuby.dll などのいくつかの DLL が含まれています。

.NET プロジェクトで IronRuby.dll が参照されたら、*.rb ファイルのオブジェクトとメソッドを C# コードに公開するにはどうすればよいですか?

どうもありがとう、

マイケル

4

1 に答える 1

5

これが相互運用の方法です。

IronRuby、、およびへIronRuby.Librariesの参照があることを確認してくださいMicrosoft.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();


        }
    }
}

Runtimeには、ファイルの実行に使用できるExecuteFileがあることに注意してください。

宝石を動かすには

  1. 必ず使用してgemをインストールしてくださいigem.exe
  2. おそらく、Engine.SetSearchPathsを使用していくつかの検索パスを設定する必要があります
于 2009-09-17T22:39:05.197 に答える