Posts
202
Comments
1112
Trackbacks
51
C# 4.0 dynamic keyword with IronRuby

In my continued exploration of C# 4.0, I wanted to put together a simple example of using the C# 4.0 “dynamic” keyword in conjunction with IronRuby.  I’ve been shocked at how difficult it was to find code samples on the internet that actually do this.  With much persistence (and some significant pointing in the right direction from Justin Etheredge) I got my simple example working.  Given that there aren’t many great examples of calling IronRuby from C# 4.0 out there, I figured I better post the code.

First off, you need to get the *correct* version of IronRuby that will work with VS2010 Beta 1.  Next, you want to add the following references to your project (all found in the “bin” folder of the IronRuby download):

  • IronRuby.dll
  • IronRuby.Libraries.dll
  • Microsoft.Scripting.dll

Next, create an IronRuby file called “person.rb” that has the following code:

   1:  class Person
   2:      def say_hello
   3:          puts "Hello world!"
   4:      end
   5:  end

Finally, here is the C# code:

   1:  ScriptRuntime runtime = IronRuby.Ruby.CreateRuntime();
   2:  ScriptScope scope = runtime.ExecuteFile("person.rb");
   3:  dynamic person = scope.Engine.Execute("p = Person.new");
   4:  person.say_hello();

Notice the use of the “dynamic” keyword on line 3.  While a full discussion of the dynamic keyword is beyond the scope of this blog post, this keyword is the basis for the dynamic features of C# 4.0 that will be running on top of the Dynamic Language Runtime (DLR) of .NET 4.0.  It will provide a unified way to interact with all things dynamic.  These include:

  • Dynamic languages like IronRuby, IronPython, and JavaScript
  • Simplified .NET Reflection
  • COM Interop
  • DOM objects like HMTL and XML

I’ll be speaking at CapArea at the end of this month where the last portion of my talk will be dedicated to C# 4.0 new language features.  Additionally, I’ll be devoting an entire talk to C# 4.0 at CMAP in November.

posted on Wednesday, August 5, 2009 2:51 PM Print
Comments
Gravatar
# re: C# 4.0 dynamic keyword with IronRuby
Mohammad Azam
8/6/2009 1:52 AM
I have written several articles and also created screencasts about IronRuby. You can check out the articles using the link below:

http://www.highoncoding.com/Categories/39_IronRuby.aspx

Thanks,
Azam

Post Comment

Title *
Name *
Email
Comment *  
 

View Steve Michelotti's profile on LinkedIn

profile for Steve Michelotti at Stack Overflow, Q&A for professional and enthusiast programmers




Google My Blog

Tag Cloud