Re: Looking for documentation about special clr related methods

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

Wow that’s just awesome!

Thanks so much Jimmy!!!


Shay F.
http://www.ironshay.com
Follow me: http://twitter.com/ironshay

Found one mistake already :slight_smile:

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
Follow me: http://twitter.com/ironshay

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
Follow me: http://twitter.com/ironshay