简体   繁体   中英

IronRuby and C#, Execute File

Why this work:

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

and this is not work

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

Try this

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

Also wrap your code in try/catch statement and check for exception

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM