# Returning a Clr type in IronRuby

**URL:** <https://www.ruby-forum.com/t/returning-a-clr-type-in-ironruby/161900>\
**Category:** IronRuby\
**Created:** [March 3, 2009, 4:18pm UTC](https://www.ruby-forum.com/t/returning-a-clr-type-in-ironruby/161900 "2009-03-03T16:18:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Cowan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ce7236/32.png) [@Paul\_Cowan](https://www.ruby-forum.com/u/Paul_Cowan)\
**Post date:** [March 3, 2009, 4:18pm UTC](https://www.ruby-forum.com/t/returning-a-clr-type-in-ironruby/161900/1 "2009-03-03T16:18:24Z")

</div>

Hi,  
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.Runtime.LoadAssembly(typeof (BuildMetaData).Assembly);  
engine.ExecuteFile(buildFile);  
var klass = engine.Runtime.Globals.GetVariable(“MetaDataFactory”);  
var instance = (RubyObject)engine.Operations.CreateInstance(klass);  
var metaData =  
(BuildMetaData)engine.Operations.InvokeMember(instance,  
“return\_meta\_data”);  
Assert.Equal(metaData.Description, “A description of sorts”);  
}

It fails when trying to cast the object returned from IronRuby with the  
following error.  
System.InvalidCastException : [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\gjpo5svj.0zd\Horn.Dsl.Specificatioin\assembly\dl3\6d8ae49c\7acadf2b\_c798c901\Horn.Core.DSL.Domain.DLL’.

The only way I can access the members of the BuildMetaData is using  
reflection.

If you look at the error message, both types are created in different  
locations.

Is this possible in IronRuby yet?

Cheers

Paul

---

<div class="post-metadata">

**Author:** ![Paul\_Cowan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ce7236/32.png) [@Paul\_Cowan](https://www.ruby-forum.com/u/Paul_Cowan)\
**Post date:** [March 3, 2009, 7:26pm UTC](https://www.ruby-forum.com/t/returning-a-clr-type-in-ironruby/161900/2 "2009-03-03T19:26:25Z")

</div>

The problem is that CLR/Mono assembly loader loads the assembly twice  
into 2 different contexts: “LoadNeither” and “Default”.  
Therefore there are actually 2 different types “BuildMetaData” in  
system.

require ‘Horn.Core.DSL.Domain’ uses Assembly.LoadFrom to load the  
assembly, which loads it to one context.  
A static assembly reference (in C#) is loaded via Assembly.Load, which  
loads to a different context.

You can use Kernel#load\_assembly or require ‘’ to  
invoke Assembly.Load.

Tomas

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Paul Cowan  
Sent: Tuesday, March 03, 2009 6:53 AM  
To: removed\_email\_address@domain.invalid  
Subject: [Ironruby-core] Returning a Clr type in IronRuby

Hi,  
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.Runtime.LoadAssembly(typeof (BuildMetaData).Assembly);  
engine.ExecuteFile(buildFile);  
var klass = engine.Runtime.Globals.GetVariable(“MetaDataFactory”);  
var instance = (RubyObject)engine.Operations.CreateInstance(klass);  
var metaData =  
(BuildMetaData)engine.Operations.InvokeMember(instance,  
“return\_meta\_data”);  
Assert.Equal(metaData.Description, “A description of sorts”);  
}

It fails when trying to cast the object returned from IronRuby with the  
following error.  
System.InvalidCastException : [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\gjpo5svj.0zd\Horn.Dsl.Specificatioin\assembly\dl3\6d8ae49c\7acadf2b\_c798c901\Horn.Core.DSL.Domain.DLL’.

The only way I can access the members of the BuildMetaData is using  
reflection.

If you look at the error message, both types are created in different  
locations.

Is this possible in IronRuby yet?

Cheers

Paul

---

<div class="post-metadata">

**Author:** ![Paul\_Cowan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ce7236/32.png) [@Paul\_Cowan](https://www.ruby-forum.com/u/Paul_Cowan)\
**Post date:** [March 3, 2009, 8:53pm UTC](https://www.ruby-forum.com/t/returning-a-clr-type-in-ironruby/161900/3 "2009-03-03T20:53:11Z")

</div>

I was running the test using the R# test runner and once I turned off  
the  
shadow-copy assemblies being tested option then the test now passes.

2009/3/3 Tomas M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
