Получение исключения при вызове метода IronRuby из кода C # - PullRequest
0 голосов
/ 18 марта 2011

Я создаю экземпляр класса ruby ​​и вызываю метод экземпляра из класса ruby ​​через программу на C #. Я могу создать экземпляр класса. Во время отладки я могу заметить метод экземпляра. Но как только я это называю, я получаю исключение RunTimeBinder. Чтобы добавить, я сослался на все библиотеки DLR. Я работаю над 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: 
...