# Re: Looking for documentation about special clr related methods

**URL:** <https://www.ruby-forum.com/t/re-looking-for-documentation-about-special-clr-related-methods/169995>\
**Category:** IronRuby\
**Created:** [June 11, 2009, 2:09am UTC](https://www.ruby-forum.com/t/re-looking-for-documentation-about-special-clr-related-methods/169995 "2009-06-11T02:09:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsoho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@jsoho](https://www.ruby-forum.com/u/jsoho)\
**Post date:** [June 11, 2009, 2:09am UTC](https://www.ruby-forum.com/t/re-looking-for-documentation-about-special-clr-related-methods/169995/1 "2009-06-11T02:09:01Z")

</div>

Not docs yet, but they are coming. They are all for better .NET  
integration. Let me give you a little bit of the docs I’m working on:

to\_clr\_type is to get the actual CLR Type object from a constant, if  
that constant represents a CLR Type. For example:

> > > System::String.class  
> > > =\> Class

In .NET, String is of type System.RuntimeType, but Ruby says it’s a  
Class, because it is mapped to a Ruby class. To get the actual CLR type,  
you use to\_clr\_type:

> > > System::String.to\_clr\_type.class  
> > > =\> System::RuntimeType

clr\_member is the get the actual member (property, field, method, etc)  
from a .NET type. When you do System::String.method(:Replace), you’ll  
get a method object pointing to the “Replace” method on Ruby’s String  
class. However, you can’t be sure it’s the method from System::String  
since “Replace” could have been monkey-patched on Ruby’s String.  
clr\_member will ensure you get the method object from the CLR Type.

> > > class System::String  
> > > … def Replace(a, b)  
> > > … raise “BOOM”  
> > > … end  
> > > … end  
> > > =\> nil
> 
> > > System::String.new(“a”).method(:Replace).call(“a”, “b”)  
> > > :2:in `Replace’: BOOM (RuntimeError)  
> > > from :0

> > > System::String.new(“a”).clr\_member(:Replace).call(“a”, “b”)  
> > > =\> ‘b’

clr\_ctor lets you explicitly call a CLR types constructor. There are  
some CLR types which map directly to Ruby types (for example:  
System.Threading.Thread -\> Thread … call “Thread.to\_clr\_type”). So:

> > > System::Threading::Thread.new  
> > > :0:in `start’: must be called with a block (ThreadError)  
> > > from :0

Eek! What happened? Because the classes are mapped, this actually calls  
Ruby’s Thread constructor. Doing System::Threading::Thread.clr\_ctor will  
give you the method object for the actual CLR constructor, allowing you  
to call it directly.

to\_clr\_string is a convenience method on String which is basically this:

class String  
def to\_clr\_string  
System::String.new(self)  
end  
end

This is needed since a Ruby string is mutable, and a CLR string is  
immutable, so they don’t map on to each other. You can still call any  
immutable Ruby methods on a CLR string, but they are different types. So  
for interop with CLR code which expects a CLR string, it’s useful to do  
“foo”.to\_clr\_string. However, I think we do conversions between these  
today, it’s not necessary, but still good to have.

Hope that helps. Let me know if you need any further clarification.  
Also, if anyone sees anything incorrect, please let me know, as I just  
wrote it =)

~js

---

<div class="post-metadata">

**Author:** ![jsoho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@jsoho](https://www.ruby-forum.com/u/jsoho)\
**Post date:** [June 11, 2009, 6:20am UTC](https://www.ruby-forum.com/t/re-looking-for-documentation-about-special-clr-related-methods/169995/2 "2009-06-11T06:20:38Z")

</div>

Wow that’s just awesome!

Thanks so much Jimmy!!!

* * *

Shay F.  
[http://www.ironshay.com](http://www.ironshay.com)  
Follow me: [http://twitter.com/ironshay](http://twitter.com/ironshay)

---

<div class="post-metadata">

**Author:** ![jsoho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@jsoho](https://www.ruby-forum.com/u/jsoho)\
**Post date:** [June 11, 2009, 7:21pm UTC](https://www.ruby-forum.com/t/re-looking-for-documentation-about-special-clr-related-methods/169995/3 "2009-06-11T19:21:55Z")

</div>

Found one mistake already 🙂

clr\_constructor and clr\_ctor return a Method object representing the  
constructors (clr\_members and overloads can then be used in order to  
further investigate the constructors).

* * *

Shay F.  
[http://www.ironshay.com](http://www.ironshay.com)  
Follow me: [http://twitter.com/ironshay](http://twitter.com/ironshay)

---

<div class="post-metadata">

**Author:** ![jsoho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@jsoho](https://www.ruby-forum.com/u/jsoho)\
**Post date:** [June 11, 2009, 7:08pm UTC](https://www.ruby-forum.com/t/re-looking-for-documentation-about-special-clr-related-methods/169995/4 "2009-06-11T19:08:45Z")

</div>

Hey,

After further investigation I have one correction and two more  
additions:

clr\_constructor and clr\_ctor - they do not execute the CLR constructor,  
they return an array of ConstructorInfo objects (one for each  
constructor ocerload).

clr\_new - executes the CLR constructor.

The Method class retrieves two new methods as well:  
clr\_members - returns an array of RunTimeMethodInfo, one for each method  
overload.

overloads - given types as arguments, it returns the matching overloaded  
CLR method.

Corrections/Comments/Treats/LotsOfMoney will be gladly accepted.

Thanks,  
Shay

* * *

Shay F.  
[http://www.ironshay.com](http://www.ironshay.com)  
Follow me: [http://twitter.com/ironshay](http://twitter.com/ironshay)
