本文介绍了从IronRuby返回CLR类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图返回从铁Ruby的CLR对象。

I am trying to return a CLR object from Iron Ruby.

我在C#中定义的下列CLR类型

I have the following CLR type defined in C#

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



我有以下的IronRuby文件:

I have the following IronRuby file:

$:.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的返回的对象时。

It fails when trying to cast the object returned from IronRuby.

我收到以下错误信息:

[A] Horn.Core.DSL.Domain.BuildMetaData不能转换为[ B] Horn.Core.DSL.Domain.BuildMetaData。类型从起源Horn.Core.DSL.Domain,版本= 1.0.0.0,文化=中立,公钥=空'在位置上下文LoadNeither''C:\Projects\horn\branches\rubydsl \src\Horn.Dsl.Specificatioin\bin\Debug\Horn.Core.DSL.Domain.dll。 B型从上下文'默认'Horn.Core.DSL.Domain,版本= 1.0.0.0,文化=中立,公钥=空'在位置'C来源:\Users\paul.cowan\AppData\\ \\Local\Temp\1vt2usw2.rxf\Horn.Dsl.Specificatioin\assembly\dl3\1d5ed945\7c19e429_1a97c901\Horn.Core.DSL.Domain.DLL。

[A]Horn.Core.DSL.Domain.BuildMetaData cannot be cast to [B]Horn.Core.DSL.Domain.BuildMetaData. Type A originates from 'Horn.Core.DSL.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location 'C:\Projects\horn\branches\rubydsl\src\Horn.Dsl.Specificatioin\bin\Debug\Horn.Core.DSL.Domain.dll'. Type B originates from 'Horn.Core.DSL.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Users\paul.cowan\AppData\Local\Temp\1vt2usw2.rxf\Horn.Dsl.Specificatioin\assembly\dl3\1d5ed945\7c19e429_1a97c901\Horn.Core.DSL.Domain.DLL'.

是否有可能从钢铁红宝石返回CLR类型

Is it possible to return CLR types from Iron Ruby

推荐答案

其实这一切归因于影子复制

Actually it was all down to shadow copying.

我的代码最终看上去是这样的:

My code ended up looking like this:

[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#测试运行则现在测试通过了。

But if I turned off shadow-copying from the R# test runner then the test now passes.

这篇关于从IronRuby返回CLR类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 10:55