IronRuby 和 C#,执行文件

发布于 2024-12-01 14:00:43 字数 1270 浏览 1 评论 0原文

为什么这有效:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月牙弯弯 2024-12-08 14:00:43

试试这个

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文