从 IronRuby 返回 CLR 类型

发布于 2024-07-13 18:04:53 字数 1655 浏览 8 评论 0原文

我正在尝试从 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 源自位置“C:\Projects\horn\branches\rubydsl\src\Horn”上下文“LoadNeither”中的“Horn.Core.DSL.Domain,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null” .Dsl.Specificatioin\bin\Debug\Horn.Core.DSL.Domain.dll'。 类型 B 源自位置“C:\Users\paul.cowan\AppData\Local\Temp”上下文“Default”中的“Horn.Core.DSL.Domain,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null” \ 1vt2usw2.rxf \ Horn.Dsl.Specificatioin \ assembly \ dl3 \ 1d5ed945 \ 7c19e429_1a97c901 \ Horn.Core.DSL.Domain.DLL'。

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

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

I have the following CLR type defined in C#

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

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

I have the following test that is failing:

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

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

I get the following error message:

[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'.

Is it possible to return CLR types from Iron Ruby

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

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

发布评论

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

评论(2

断桥再见 2024-07-20 18:04:53

而不是使用特制的 Ruby 字符串获取方法,然后使用 C# 调用该方法。

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")

从 C# 调用 Ruby 代码的首选方法如下: ~Jimmy,

Rather than getting the method with a specially crafted string of Ruby, and then using C# to call the method, the preferred way to call into Ruby code from C# is the following:

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")

~Jimmy

岁月染过的梦 2024-07-20 18:04:53

事实上,这一切都归咎于卷影复制。

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

[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# 测试运行器关闭卷影复制,那么测试现在就通过了。

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

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

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