Intro to IronRuby/DLR Scripting in C# Silverlight 4 Application

29. September 2010

I’ve been working lately on figuring out how to add DLR scripting support to a Silverlight 4 application I’ve been working on. The idea is to give it a plugin style architecture, with the plugins being written in a DLR language; currently focusing on IronRuby. I originally searched how to do this, but couldn’t find any recent articles that discuss Silverlight 4. Also, most articles out don’t target Silverlight and more of them discuss IronPython and not IronRuby, even though the DLR stuff is the same regardless of DLR language used.

So, I thought I’d share what I’m learning about adding IronRuby scripting to Silverlight 4 applications. I’m also focusing on C# as the host language, but the .NET object and methods used will be the same from VB.NET.

Older Articles for Reference

What to download?

Since I am talking about using Silverlight 4, you will need Visual Studio 2010 installed.

You will need to have the following installed:

Also, download the following and extract, as it will be used later:

Add Assembly References to Silverlight Project

Either create a new Silverlight 4 project or use an existing one, then add the references to the following assemblies located within the “silverlight/bin” folder of the IronRuby 1.1 archive previously downloaded.

  • IronRuby.dll
  • IronRuby.Libraries.dll
  • IronRuby.Libraries.Yaml.dll
  • Microsoft.Dynamic.dll
  • Microsoft.Scripting.Debugging.dll
  • Microsoft.Scripting.dll
  • Microsoft.Scripting.Silverlight.dll
  • System.Numerics.dll

If your using a C# project, then you will also need to Add Reference to Microsoft.CSharp namespace.

Execute IronRuby Code from C#

It is actually very simple to use the ScriptEngine class to execute DLR code from within C# (or any other .NET language.)

Here’s a small example that show how to use ScriptEngine to execute a small piece of IronRuby code:

// Create instance of the IronRuby ScriptEngine
ScriptEngine engine = Ruby.CreateEngine();

// some IronRuby code to execute
// this just adds 2 plus 2
var code = "2 + 2";

// Execute the code and catch the result
// in a variable
var result = engine.Execute(code);

// After execution "result" should contain
// the value "4"

As you can see, it really is only 2 lines of code required to execute a piece of IronRuby (or other DLR language) code.

Use Global Variables to Grant Access Within Your Application

The ScirptEngine class allows you to set global variables from .NET code to allow the DLR language (in this case IronRuby) access to them.

Here’s an example of creating a global variable “MainPage” that is a reference to the object that is executing the code. The MainPage object also implements a method named “SetTitleText” that the IronRuby code will call.

public partial class MainPage : UserControl
{
    public void SetTitleText(string t)
    {
        this.txtTitle.Text = t;
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        ScriptEngine engine = Ruby.CreateEngine();

        // Set global variable for access to this Page instance
        // The Ruby code will only be able to access the "MainPage"
        // global variable
        engine.Runtime.Globals.SetVariable("MainPage", this);

        // Use Ruby to set the text displayed in a TextBlock using a method
        // exposed by this Page object
        string code = "MainPage.SetTitleText(\"Hello from IronRuby!\")";
        engine.Execute(code);
    }
}

Using this you can grant the DLR script access to any objects or methods within your application.

Return Object Instance from IronRuby and Execute a Method from C#

Here’s another code example of executing IronRuby code from C#. This time the IronRuby code is creating an instance of an object and returning that object so that C# can call one of its methods.

ScriptEngine engine = Ruby.CreateEngine();

// Execute IronRuby code that creates an
// instance of a class
dynamic obj = engine.Execute(@"
    class MyObject
        def add(a, b)
            a + b
        end
    end
    MyObject.new
");

// call the "add" method of the IronRuby object
var i = obj.add(1, 2);

// Display the result of the IronRuby method to
// the user
HtmlPage.Window.Alert("i = " + i);

 

Conclusion

The examples shown here may seem really simple and not to offer much, but this is just the basics of what can allow you to add a ton of power to your applications. Being able to easily script certain pieces of your application without the need to recompile and redeploy the solution can drastically increase the speed at which certain application changes, such as business logic, can be implemented. This also allows for a much more modular plug-in model to be developed in to the application; which is what I’m looking to do.

Silverlight, DLR, Ruby , , ,

Comments

10/4/2010 4:45:48 PM #
Intro to IronRuby/DLR Scripting in C# Silverlight 4 Application

You've been kicked (a good thing) - Trackback from DotNetKicks.com
10/4/2010 5:38:48 PM #
Pingback from sciencereportnow.com

Intro to IronRuby/DLR Scripting in C# Silverlight 4 Application | Science Report | Biology News, Economics News, Computer Science News, Mathematics News, Physics News, Psychology News
10/7/2010 12:44:12 AM #
Silverlight: Embed IronRuby/DLR Scripting within XAML using IValueConverter and Custom UserControl

Silverlight: Embed IronRuby/DLR Scripting within XAML using IValueConverter and Custom UserControl
10/9/2010 9:26:57 AM #
cheers
10/9/2010 9:58:34 AM #
Where is your rss? I cant find it
10/9/2010 10:05:18 AM #
a piece of spam
10/9/2010 10:18:05 AM #
This is a great blog, I've loved having a look around today.  I'm sure you could definitely make a dollar or two if you sold some more advertising on here!
10/9/2010 10:58:08 AM #
mmmm tasty
10/9/2010 11:28:00 AM #
mmmm tasty
10/9/2010 12:29:55 PM #
Matchless phrase ;)
10/9/2010 4:07:57 PM #
This is a very interesting post. Thank you for posting this post.
10/9/2010 6:05:15 PM #
Thanks for posting this! i really enjoyed reading this.
10/9/2010 8:48:57 PM #
I'm happy to come accross some relevant information here. This business tends to result in people writing the same old tired stuff throughout the web.
10/10/2010 8:56:27 AM #
If I might --perhaps you should consider adding a few images.  I dont mean to disrespect what youve said; its very enlightening, indeed.  However, I think would respond to it more positively if they could be something tangible to your ideas.  Keep it up, but put a little more into it next time.
10/10/2010 9:05:34 AM #
I should really be working
10/10/2010 10:05:32 AM #
Hey check out my blog too. I hope i have some fun stuff too
10/10/2010 10:09:11 AM #
great site.. nice layout
10/10/2010 7:37:31 PM #
Hey guy, was just browsing by means of the world wide web wanting for some data and came throughout your website. I am impressed by the info that you have on this blog site. It exhibits how effectively you understand this subject. Bookmarked this page, will arrive again for more. You, my friend, ROCK!!!
10/10/2010 10:12:23 PM #
When people talk about this concern, I sometime believe that they just not genuinely explain it thoroughly adequate. I can comprehend your post properly as it’s truly properly written.
10/10/2010 10:14:50 PM #
Hello! totally liked the last post. keep up the good work!!
10/11/2010 3:38:14 PM #
Plus sized clothing can be very hard to find, so many stores that just don't carry anything nice these days, whatever.. i guess none of them really did much to begin with but.. i guess its all ok . eh, anyways, thats just for me lol,
10/11/2010 4:51:49 PM #
Quite interesting post.  Your own website is swiftly becoming among my favorites.
10/11/2010 8:37:51 PM #
Very worthwhile posting.  Your current web is swiftly starting to be considered one of my favorites.
10/11/2010 9:16:27 PM #
Plus sized clothing can be hard to find, so many stores that just don't carry anything nice these days, well.. i guess none of them really did much to begin with but.. i guess its all fine . eh, anyways, thats just for me lol,
10/11/2010 9:21:45 PM #
Thanks for an fascinating and stimulating weblog post.
10/11/2010 10:47:29 PM #
Here is the 2nd instance I have noticed your internet site within the last few weeks.  Seems like I should take note of it.
10/11/2010 10:51:59 PM #
Fabulous blog post, lots of high-quality information and facts.  I am going to show my buddies and ask them what they think.
10/11/2010 11:14:16 PM #
I really like when people are expressing their thoughts and opinions on things. So I like the way you are writing
10/12/2010 2:40:16 AM #
Awesome article, can u publish ?
10/12/2010 2:59:43 AM #
seriously? this kinda reminds me of poker haha, kinda makes you think about how many of these people are bluffing, i mean, for instance, the guy above me, he is obviously spam, but trys so hard, sometimes i don't really get it though, what is the point of the whole spam thing? eh, whatever, i guess you can't stop them, but its still funny.
10/12/2010 3:06:10 AM #
Quite interesting posting.  Your own site is swiftly starting to be among my top picks.
10/12/2010 5:08:31 AM #
This is the second moment I have noticed your blog within the last couple weeks.  Seems like I ought to take note of it.
10/12/2010 6:56:28 AM #
When people talk about this concern, I sometime believe that they just not genuinely explain it thoroughly adequate. I can comprehend your post properly as it’s truly properly written.
10/12/2010 7:15:49 AM #
This is actually the 2nd moment I have run into your website within the last few weeks.  Seems like I should take note of it.
10/12/2010 8:15:08 AM #
Probably the most succinct and also current tips I stumbled upon on that topic. Sure lucky that I discovered that web site by chance. I’ll probably be signing up to your own rss feed so that I will have the current updates. Love all the stuff here.
10/12/2010 8:37:32 AM #
Just now I discovered the web for this information. I thank you for your efforts this journey has an end  now. You wrote the post in a very understandable way. So I want to say thanks to you and add your blog to my favorites just now. Enjoy the daytime
10/12/2010 1:14:40 PM #
Just wanted to say hello from New York
10/12/2010 4:50:59 PM #
This is really beautiful!
10/12/2010 7:03:10 PM #
I liked this article
10/12/2010 7:18:31 PM #
Have you ever considered adding more videos to your blog posts to keep the readers more entertained? I mean I just read through the entire article of yours and it was quite good but since I'm more of a visual learner,I found that to be more helpful well let me know how it turns out
10/13/2010 7:51:52 AM #
Hey check out my blog too. I hope i have some fun stuff too
10/13/2010 10:51:36 AM #
Really liked your post.  Discovered the article via google.  The real estate market is the only thing that is going to turn around the overall economy. Especially in Phoenix.
10/13/2010 12:15:13 PM #
I admire the time and effort you put into your blog and detailed information you offer! I will bookmark your blog and check up here often. Thumbs up!
10/13/2010 12:25:45 PM #
Notice the Reasons of the Popularity of http://www.bagsok.com http://www.bagsok.com Most First-Class Gifts in the Season of Summer
10/13/2010 2:47:25 PM #
I mean, awesome site. Congratulations.
10/13/2010 6:21:19 PM #
I mean, awesome post. I like read your blog.
10/13/2010 8:52:04 PM #
Ay�deme, por favor Hola webmaster, buen d�a.
10/13/2010 11:27:16 PM #
super
10/13/2010 11:38:32 PM #
Wonderful blog post, a bunch of good details.  I want to show my friends and ask them the things they think.
10/13/2010 11:41:58 PM #
Here is the 2nd occasion I've noticed your web site within the last couple weeks.  Seems as if I ought to take note of it.
10/14/2010 2:18:16 AM #
Its helpful article. I will visit again soon!
10/14/2010 7:57:56 AM #
Thanks for the info, I really need to get rid of my cellulite.
10/14/2010 8:09:19 AM #
I think nice article. Well done.
10/14/2010 5:00:35 PM #
hi thank you for this wonderful informative blog
10/14/2010 8:18:16 PM #
A panic attack is very treatable. What you need to do first and foremost is accept that you have a little problem, a problem that does not let you perceive certain painful situations in the right way.
10/14/2010 8:24:30 PM #
I'd prefer to thank you for your efforts you've produced in composing this posting. I'm heading for that exact perfect give good results from you inside long term as effectively. In actuality your fanciful composing capabilities has prompted me to start out my personal web site now. In fact the blogging is spreading its wings swiftly. Your write up can be a good illustration of it.
10/14/2010 9:16:41 PM #
After reading this I thought it was very enlightening. I appreciate you taking the time and effort to put this piece together. Once again I find myself spending way to much time both reading and commenting. But who cares, it was still worth it!