问题
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
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/585047/returning-a-clr-type-from-ironruby