[Ironruby-core] Calling .Net generic method with Ruby object

Orion Edwards Orion.Edwards at gallagher.co
Wed Jul 18 20:58:44 UTC 2012

The .NET method can't accept normal IronRuby objects, because of the new() 

IronRuby objects behind the scenes are all instances of A .NET class 
called RubyObject - you can see it's code here:  

RubyObject has 2 constructors, both of which require parameters... Your 
new() constraint says "I can only accept methods with public zero-argument 
constructors"... RubyObject doesn't satisfy this (and neither do many many 
other .NET classes), so it simply doesn't work.

If you remove the new() constraint, you can pass Ruby objects to the Store 

There is another way to do this also. If you create a blank .NET class 
with a default constructor, and have your ruby classes derive from it, 
then IronRuby will create instances of those classes instead of 
You can then call the Store method and use the .NET class as the T.
Remember, C# can't see any of the ruby methods, but it will also allow you 
to "store" the object.

Here's my example code:


public static class Demo
    public static void Store<T>(T obj) where T : class, new()
        Console.WriteLine("Storing {0}", obj);

public class SimpleObject
    public SimpleObject() { }


class X < SimpleObject; end

x_instance = X.new
Demo.method(:Store).of(SimpleObject).call x_instance

# This prints "Storing IronRuby.Classes.SimpleObject$1"

If it's ok, can I ask what you're trying to do here? Normal .NET code 
can't really interact with ruby objects unless it's built to use the C# 4 
dynamic keyword, or has special knowledge of the Dynamic Language Runtime 
API's... If you're just putting ruby objects in a list so that ruby code 
can retrieve them later that will be fine, but if you're trying to do 
other things, you may run into problems...


Orion Edwards | Technical Leader 
PHONE +64 7 838 9800 | FAX +64 7 838 9801 | 
EMAIL orion.edwards at gallagher.co | WEB www.gallagher.co  

From:   Alexander Ranger <lists at ruby-forum.com>
To:     ironruby-core at rubyforge.org
Date:   18/07/2012 06:23 p.m.
Subject:        [Ironruby-core] Calling .Net generic method with Ruby 
Sent by:        ironruby-core-bounces at rubyforge.org

Hello. I've got this problem:

I'm having a .Net method with generics like this:

void Store<T>(T obj) where T : class, new();

And I'm trying to call it from IronRuby project with Ruby object as an

comp = Computer.new
#session is another user created .Net object, which has the method Store

And when I'm running the project an error pops up
"GenericArguments[0], "IronRuby.Builtins.RubyObject", in "Void
Store[T](T)" does not satisfy the restriction of the type parameter "T""

I guess that .Net generic just can't accept RubyObject as an argument.
But can I somehow bypass that restriction?

Posted via http://www.ruby-forum.com/.
Ironruby-core mailing list
Ironruby-core at rubyforge.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20120719/0f5979c9/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/gif
Size: 3465 bytes
Desc: not available
URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20120719/0f5979c9/attachment-0001.gif>

More information about the Ironruby-core mailing list