2011-08-28 49 views
0

爲什麼這一工作:IronRuby和C#,執行文件

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.IO; 
using Microsoft.Scripting; 
using Microsoft.Scripting.Hosting; 
using IronRuby; 

class Tutorial 
{ 
    static void Main(string[] args) 
    { 
     var engine = Ruby.CreateEngine(); 
     engine.Runtime.Globals.SetVariable("Holly",new Holly("Test")); 
     engine.Execute("Holly.Say"); 
     Console.ReadLine(); 
    } 
} 
class Holly 
{ 
    string speech; 
    public Holly(string speech) 
    { 
     this.speech = speech; 
    } 
    public void Say() 
    { 
     Console.WriteLine("Hollu said " + this.speech); 
    } 
} 

,這是不行的

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.IO; 
using Microsoft.Scripting; 
using Microsoft.Scripting.Hosting; 
using IronRuby; 

class Tutorial 
{ 
    static void Main(string[] args) 
    { 
     var engine = Ruby.CreateEngine(); 
     engine.Runtime.Globals.SetVariable("Mouse",new Holly("Test")); 
     engine.ExecuteFile("./test.rb"); 
     Console.ReadLine(); 
    } 
} 
class Holly 
{ 
    string speech; 
    public Holly(string speech) 
    { 
     this.speech = speech; 
    } 
    public void Say() 
    { 
     Console.WriteLine("Hollu said " + this.speech); 
    } 
} 
+2

我的第一個猜測是沒有找到'。/ test.rb' - 你得到了什麼錯誤? – dahlbyk

+0

它不起作用? – svick

回答

0

試試這個

engine.ExecuteFile(@"..\test.rb"); 

而且包裝你的代碼在try/catch語句並檢查異常

try 
{ 
    var engine = Ruby.CreateEngine(); 
    engine.Runtime.Globals.SetVariable("Holly",new Holly("Test")); 
    engine.ExecuteFile(@"..\test.rb"); 
} 
catch (Exception ex) 
{ 
    Console.WriteLine(ex.Message); 
} 
+0

engine.ExecuteFile(@「.. \ test.rb」);不行。異常:NoImplementedException – harungo

+0

檢查了這一點http://stackoverflow.com/questions/585047/returning-a-clr-type-from-ironruby 你也可以嘗試指定您的test.rb文件的完整傳球。如engine.ExecuteFile(@「c:\ test \ test.rb」); –