كيف يمكنني استخدام كود روبي في .NET؟
سؤال
أرغب في استخدام RubyGem في تطبيق C# الخاص بي.
لقد قمت بتنزيل IronRuby، لكني لست متأكدًا من كيفية تشغيله.يتضمن تنزيلها ir.exe، ويتضمن بعض ملفات DLL مثل IronRuby.dll.
بمجرد الإشارة إلى IronRuby.dll في مشروع .NET الخاص بي، كيف يمكنني كشف كائنات وأساليب ملف *.rb إلى كود C# الخاص بي؟
شكرا جزيلا،
ميخائيل
المحلول
هذه هي الطريقة التي تقوم بها بالتداخل:
تأكد من أن لديك مراجع ل IronRuby
, IronRuby.Libraries
, Microsoft.Scripting
و 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();
}
}
}
لاحظ أن Runtime يحتوي على ملف ExecuteFile الذي يمكنك استخدامه لتنفيذ ملفك.
للحصول على الأحجار الكريمة الذهاب
- تأكد من تثبيت الجوهرة الخاصة بك باستخدام
igem.exe
- ربما يتعين عليك تعيين بعض مسارات البحث باستخدام Engine.SetSearchPaths
لا تنتمي إلى StackOverflow