【问题标题】:IronRuby and C#, Execute FileIronRuby 和 C#,执行文件
【发布时间】:2011-08-28 17:50:49
【问题描述】:

为什么会这样:

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);
   }
}

【问题讨论】:

  • 我的第一个猜测是找不到./test.rb - 你得到什么错误?

标签: c# ironruby


【解决方案1】:

试试这个

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);
}

【讨论】:

  • engine.ExecuteFile(@"..\test.rb");不行。异常:NoImplementedException
  • 检查一下stackoverflow.com/questions/585047/… 您是否也可以尝试为您的 test.rb 文件指定完全通过。如engine.ExecuteFile(@"c:\test\test.rb");
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-09
  • 1970-01-01
  • 2014-05-15
相关资源
最近更新 更多