【问题标题】:Returning a CLR type from IronRuby从 IronRuby 返回 CLR 类型
【发布时间】:2009-02-25 07:28:17
【问题描述】:

我正在尝试从 Iron Ruby 返回一个 CLR 对象。

我在 C# 中定义了以下 CLR 类型

public class BuildMetaData
{
    public string Description { get; set; }
}

我有以下 IronRuby 文件:

$:.unshift(File.dirname(__FILE__) + '/../bin/Debug') 
require 'mscorlib'
require 'Horn.Core.DSL.Domain'

class MetaDataFactory
    def return_meta_data()
        meta = Horn::Core::DSL::Domain::BuildMetaData.new
        meta.Description = "A description of sorts"
        meta
    end
end

我有以下测试失败:

[Fact]
public void Then_a_build_metadata_object_is_returned()
{                       
    var engine = Ruby.CreateEngine();

    engine.ExecuteFile("test.rb");

    var code = String.Format("{0}.new.method :{1}", "MetaDataFactory", "return_meta_data");

    var action = engine.CreateScriptSourceFromString(code).Execute();

    var result = (BuildMetaData)engine.Operations.Call(action);

    Assert.Equal(result.Description, "A description of sorts");
}

尝试投射 IronRuby 返回的对象时失败。

我收到以下错误消息:

[A]Horn.Core.DSL.Domain.BuildMetaData 无法转换为 [B]Horn.Core.DSL.Domain.BuildMetaData。类型 A 源自 'Horn.Core.DSL.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 在上下文 'LoadNeither' 的位置 'C:\Projects\horn\branches\rubydsl\src\Horn .Dsl.Specificatioin\bin\Debug\Horn.Core.DSL.Domain.dll'。类型 B 源自 'Horn.Core.DSL.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 在位置 'C:\Users\paul.cowan\AppData\Local\Temp 的上下文'Default' \1vt2usw2.rxf\Horn.Dsl.Specificatioin\assembly\dl3\1d5ed945\7c19e429_1a97c901\Horn.Core.DSL.Domain.DLL'。

是否可以从 Iron Ruby 返回 CLR 类型

【问题讨论】:

    标签: c# ironruby


    【解决方案1】:

    与其通过特制的 Ruby 字符串获取方法,然后使用 C# 调用该方法,不如从 C# 调用 Ruby 代码的首选方法如下:

    var engine = IronRuby.Ruby.CreateEngine()
    engine.ExecuteFile("test.rb")
    var klass = engine.Runtime.Globals.GetVariable("MetaDataFactory")
    var instance = engine.Operations.CreateInstance(klass)
    engine.Operations.InvokeMember(instance, "return_meta_data")
    

    ~吉米

    【讨论】:

      【解决方案2】:

      实际上,这一切都归结为影子复制。

      我的代码最终看起来像这样:

      [Fact]
      public void Then_the_object_should_be_accessible_in_csharp()
      {                       
          var engine = Ruby.CreateEngine();
      
          engine.Runtime.LoadAssembly(typeof (BuildMetaData).Assembly);
      
          engine.ExecuteFile(buildFile);
      
          var klass = engine.Runtime.Globals.GetVariable("MetaDataFactory");
      
          var instance = (RubyObject)engine.Operations.CreateInstance(klass);
      
          //You must have shadow-copying turned off for the next line to run and for the test to pass.
          //E.g. in R# "ReSharper/Options/Unit Testing/Shadow-Copy Assemblies being tested" should be un-checked.
          var metaData = (BuildMetaData)engine.Operations.InvokeMember(instance, "return_meta_data");
      
          Assert.Equal(metaData.Description, "A description of sorts");
      
          Assert.Equal(metaData.Dependencies.Count, 1);
      }
      

      但是如果我从 R# 测试运行器中关闭影子复制,那么测试现在通过了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-02
        • 1970-01-01
        • 2011-09-06
        • 1970-01-01
        • 2013-06-21
        • 2012-08-11
        • 1970-01-01
        相关资源
        最近更新 更多