从C#代码调用IronRuby方法时获取异常

时间:2011-03-18 04:47:34

标签: c# interop ironruby

我正在实例化一个ruby类,并通过C#程序从ruby类调用实例方法。 我能够实例化该类。调试时我可以注意到实例方法。但是一旦我调用它,我就会得到RunTimeBinder异常。 要添加我已经引用了DLR的所有dll。我正在研究VS 2010。 以下源代码是我获得异常的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Scripting.Hosting;

namespace ConsoleApplication2
{
  class Program
  {
    static void Main(string[] args)
    {
        RunRubyClassInstatainateMethod();
        Console.ReadLine();
    }

    private static void RunRubyClassInstatainateMethod()
    { 
  /*WHEN PUT A BREAK POINT TO THE BELOW LINE OF CODE I AM ABLE TO SEE 
    THE print_mthd() under Instance Method list*/
        dynamic stretchstring = RubyExampleCode.CreateRubyProduct();
  /*THE BELOW CALL RESULTS IN AN EXCEPTION*/
        stretchstring.print_mthd();
    }
  }
  class RubyExampleCode
  {

    private static dynamic productClass;
    private static ScriptEngine rbEngine;
    private static dynamic loadFile;
    static RubyExampleCode()
    {
        rbEngine = IronRuby.Ruby.CreateEngine();
        loadFile = rbEngine.ExecuteFile("R.rb");
        productClass = rbEngine.Runtime.Globals.GetVariable("RubyProduct");
    }

    public static void invokeDynaMthd()
    {
        loadFile.print_mthd();
    }
    public static dynamic CreateRubyProduct()
    {
        return rbEngine.Operations.CreateInstance(productClass);
    }
  }
}

/***********************************************************************/

/*This is the ruby class definition*/
class RubyProduct
def print_mthd
puts "This is a demo"
end
end

例外情况如下:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException was unhandled
  Message='IronRuby.Builtins.RubyObject' does not contain a definition for 'abc'
  Source=Anonymously Hosted DynamicMethods Assembly
  StackTrace:
       at CallSite.Target(Closure , CallSite , Object )
       at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid1[T0](CallSite site, T0 arg0)
       at ConsoleApplication2.Program.RunRubyClassInstatainateMethod() in C:\Users\IC012722\documents\visual studio 2010\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs:line 21
       at ConsoleApplication2.Program.Main(String[] args) in C:\Users\IC012722\documents\visual studio 2010\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs:line 13
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

0 个答案:

没有答案