2011-08-28 78 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”); –