从 IronRuby 返回 CLR 类型
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
而不是使用特制的 Ruby 字符串获取方法,然后使用 C# 调用该方法。
从 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:
~Jimmy
事实上,这一切都归咎于卷影复制。
我的代码最终看起来像这样:
但如果我从 R# 测试运行器关闭卷影复制,那么测试现在就通过了。
Actually it was all down to shadow copying.
My code ended up looking like this:
But if I turned off shadow-copying from the R# test runner then the test now passes.