# CLR interop bug?

**URL:** <https://www.ruby-forum.com/t/clr-interop-bug/169874>\
**Category:** IronRuby\
**Created:** [June 9, 2009, 9:28pm UTC](https://www.ruby-forum.com/t/clr-interop-bug/169874 "2009-06-09T21:28:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![barby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@barby](https://www.ruby-forum.com/u/barby)\
**Post date:** [June 9, 2009, 9:28pm UTC](https://www.ruby-forum.com/t/clr-interop-bug/169874/1 "2009-06-09T21:28:59Z")

</div>

Hi  
What is the nitty gritty around monkey patching CLR classes. When I’m  
monkey  
patching I should be able to get to the protected members of a class  
right?  
And the private members too (I see it kind of like partial classes)?

public class AggregatorRepository :  
RepositoryBase  
{  
public  
AggregatorRepository(UnitOfWorkScopeBase  
unitOfWorkScope) : base(unitOfWorkScope)  
{  
}

```
//Some C# methods here that use Linq and Expressions for querying

```

}

Repository base defines a protected property UnitOfWorkScope

I monkey patched it like so

module MediaAggregator  
module Models  
module Lightspeed

```
  class AggregatorRepository

    def users
      uow.method(:find).of(User).call
    end

    def uow
      self.unit_of_work_scope.current
    end

  end
end

```

end  
end

Calling the users method result in an error complaining about a  
protected  
unit\_of\_work\_scope method. And ironically the error talks about my  
situation  
as being the solution:  
\*  
\*  
_C\*\*LR protected method `unit\_of\_work\_scope’ called for  
MediaAggregator.Models.Lightspeed.AggregatorRepository:MediaAggregator::Models::Lightspeed::AggregatorRepository;  
CLR protected methods can only be called with a receiver whose class is  
a  
subclass of the class declaring the method_

Do I put this on codeplex or am I reading the error wrong? As far as I  
can  
tell I am subclassing a class.

* * *

Met vriendelijke groeten - Best regards - Salutations  
Ivan Porto C.  
Blog: [http://flanders.co.nz](http://flanders.co.nz)  
Twitter: [http://twitter.com/casualjim](http://twitter.com/casualjim)  
Author of IronRuby in Action ([http://manning.com/carrero](http://manning.com/carrero))

---

<div class="post-metadata">

**Author:** ![barby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@barby](https://www.ruby-forum.com/u/barby)\
**Post date:** [June 9, 2009, 10:04pm UTC](https://www.ruby-forum.com/t/clr-interop-bug/169874/2 "2009-06-09T22:04:13Z")

</div>

The error message might be more clear:

CLR protected method `unit\_of\_work\_scope’ called for  
MediaAggregator.Models.Lightspeed.AggregatorRepository:MediaAggregator::Models::Lightspeed::AggregatorRepository;  
CLR protected methods can only be called with a receiver whose class is  
a Ruby subclass of the class declaring the method

You need to derive a Ruby class from the CLR class and use its instance  
to call the method. What we need to do internally is emit a CLR subtype  
of the CLR type that proxies protected method calls coming from Ruby  
code. When you monkey-patch a CLR method it doesnâ€™t mean you are â€œin the  
typeâ€. You are still outside of the type, so you still have the same  
visibility restrictions on CLR members as if they were called from any  
other Ruby/CLR method. IronRuby just overlays Ruby methods over the CLR  
type, it cannot â€œopenâ€ the type.

Tomas

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Ivan Porto  
Carrero  
Sent: Tuesday, June 09, 2009 12:28 PM  
To: ironruby-core  
Subject: [Ironruby-core] CLR interop bug?

Hi

What is the nitty gritty around monkey patching CLR classes. When I’m  
monkey patching I should be able to get to the protected members of a  
class right? And the private members too (I see it kind of like partial  
classes)?

public class AggregatorRepository :  
RepositoryBase  
{  
public  
AggregatorRepository(UnitOfWorkScopeBase  
unitOfWorkScope) : base(unitOfWorkScope)  
{  
}

```
//Some C# methods here that use Linq and Expressions for querying

```

}

Repository base defines a protected property UnitOfWorkScope

I monkey patched it like so

module MediaAggregator  
module Models  
module Lightspeed

```
  class AggregatorRepository

    def users
      uow.method(:find).of(User).call
    end

    def uow
      self.unit_of_work_scope.current
    end

  end
end

```

end  
end

Calling the users method result in an error complaining about a  
protected unit\_of\_work\_scope method. And ironically the error talks  
about my situation as being the solution:

CLR protected method `unit\_of\_work\_scope’ called for  
MediaAggregator.Models.Lightspeed.AggregatorRepository:MediaAggregator::Models::Lightspeed::AggregatorRepository;  
CLR protected methods can only be called with a receiver whose class is  
a subclass of the class declaring the method

Do I put this on codeplex or am I reading the error wrong? As far as I  
can tell I am subclassing a class.

* * *

Met vriendelijke groeten - Best regards - Salutations  
Ivan Porto C.  
Blog: [http://flanders.co.nz](http://flanders.co.nz)  
Twitter: [http://twitter.com/casualjim](http://twitter.com/casualjim)  
Author of IronRuby in Action ([http://manning.com/carrero](http://manning.com/carrero))
