Hosted Scripting in .Net 4
So this is just the coolest thing evar.
One of the prime motivations for Microsoft doing its IronRuby and IronPython work is as embedable languages in existing CLR applications. When I read that I was sort of underwhelmed, expecting some really hairy communication mechanisms like com interop. Boy, I was wrong.
The general idea is each dynamic language implements a ScriptEngine, which is used by the DLR hosting framework. Because it is interface based, you can work with any DLR language exactly the same way. This means that by going this route, you get support for ruby, python, scheme, etc. Which is hela-cool.
From the hosting application point of view, what you need is a ScriptRuntime (which takes a ScriptEngine). The runtime will then be able to give you a ScriptScope object, which is basically the bride that you use to communicate with the hosted script. You can register assemblies or objects with the scope, and those values become accessable from the hosted script. By the same tolken, the hosted script can register values, that can then be read from the hosting application.
Something like this
var csharp_var = "I am set from C# land";
var ruby = "csharp_land = :ruby_ftw.to_s"; // my odd ruby example
var runtime = new ScriptRuntime("rb");
var scope = runtime.CreateScope();
scope.SetVariable("csharp_land", csharp_var)
// expose the c# variable to hosted ruby
Console.WriteLine("Before script execution: " + csharp_var);
// Before script execution: I am set from C# land
runtime.Execute(ruby, scope);
Console.WriteLine("After script execution: " + csharp_var);
// After script execution: ruby_ftw
Freakin awesome.
Want to expose a library? runtime.LoadAssembly(<Assembly>). Want to get a variable from the script? scope.GetVariable(<name>). You can even get methods defined in the script in to C#, and they will be defined as Actions or Funcs, allowing you to do callbacks.
The thing that I like about this isn't so much that its possible, it is that you can implement it in about 3 lines of extremely straight forward C#. Even things like UI scripting for automated tests becomes trivial, you just expose the base object on each form and you are pretty much done; full access to the code at runtime.
I really didn't expect to be excited about this, but it really may be the highlight session for me from PDC09. If you are interested in this, here is the vid