簡體   English   中英

IronRuby和C#,執行文件

[英]IronRuby and C#, Execute File

工作原理:

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

嘗試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM