1

Closed

Explicit interface method implementation not callable

description

From RubyForge #23494, re-reported here: http://www.ruby-forum.com/topic/189305
 
C# code:
 
public interface IFoo
{
    void Bar();
}
 
public class Cls : IFoo
{
    void IFoo.Bar() { }
}
 
Ruby code:
x = App::Cls.new
x.bar
Closed Feb 28, 2010 at 6:06 AM by jredville
This works great for Explicit interfaces, but it doesn't work for nil.as(C) since a ruby TypedObject proxy isn't really a C. Closing this and re-opening the one related to nil. Specs are written and will be checked in this week.

comments

TomasMatousek wrote Oct 9, 2009 at 9:23 PM

One way how this might be implemented is to use TypedObject { object Value; Type Type; } as a holder that carries the type along with the reference. Then we can get the target interface type in the binder. Kernel#as method could return a TypedObject instance. This would also work for nil.as(C).

TomasMatousek wrote Oct 10, 2009 at 1:48 AM

The basic support for this features is now provided by Kernel#clr_members:
x.clr_member(IFoo, :bar).call
Calls method bar on object foo casted to interface IFoo.

More convenient API could be implemented on top of that in Ruby:
class TypedObject
def initialize(obj, type)
 @obj, @type = obj, type
end

def method_missing name, *args, &block
@obj.clr_member(@type, name).call(*args, &block)
end
end

module Kernel
def as(type)
 TypedObject.new(self, type)
end
end

x.as(IFoo).bar