Question

J'aimerais utiliser un RubyGem dans mon application C #.

J'ai téléchargé IronRuby, mais je ne sais pas comment me lancer. Leur téléchargement inclut ir.exe et certaines DLL telles que IronRuby.dll.

Une fois que IronRuby.dll est référencé dans mon projet .NET, comment puis-je exposer les objets et les méthodes d'un fichier * .rb à mon code C #?

Merci beaucoup,

Michael

Était-ce utile?

La solution

C’est comme ça que vous faites interop:

Assurez-vous que vous avez les références à IronRuby , IronRuby.Libraries , Microsoft.Scripting et 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();


        }
    }
}

Remarque: Runtime contient un fichier ExecuteFile que vous pouvez utiliser pour exécuter votre fichier.

Pour lancer les pierres précieuses

  1. Assurez-vous d'installer votre bijou à l'aide de igem.exe
  2. vous devrez probablement définir des chemins de recherche à l'aide de Engine.SetSearchPaths
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top