Advertisement
oldrev

Embedded IronRuby

May 29th, 2011
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.17 KB | None | 0 0
  1. using System;
  2. using System.Reflection;
  3. using Microsoft.Scripting;
  4. using Microsoft.Scripting.Hosting;
  5. using IronRuby;
  6.  
  7. namespace IronRubyTest1
  8. {
  9.     public class Human
  10.     {
  11.         protected string GetInternalCondition()
  12.         {
  13.             return "all good";
  14.         }
  15.     }
  16.  
  17.     class Program
  18.     {
  19.         static void Main(string[] args)
  20.         {
  21.             const string RubyCode = @"
  22. class Doctor < IronRubyTest1::Human
  23.  def check_condition()
  24.    puts 'Condition: ' + get_internal_condition()
  25.  end
  26. end";
  27.             ScriptRuntime runtime = Ruby.CreateRuntime();
  28.             ScriptEngine engine = Ruby.GetEngine(runtime);
  29.             ScriptScope scope = engine.CreateScope();
  30.  
  31.             //让 DLR 载入包含 Human 类的 Assembly
  32.             runtime.LoadAssembly(Assembly.GetExecutingAssembly());
  33.             engine.Execute(RubyCode, scope); //执行 Ruby 代码
  34.             dynamic globals = engine.Runtime.Globals;
  35.             dynamic house = globals.Doctor.@new(); //创建 Doctor 的实例
  36.             house.check_condition();
  37.  
  38.             Console.WriteLine("Press any key to close...");
  39.             Console.ReadKey();
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement