Either I’m incapable of finding a nice “from 0 – 60” tutorial on getting up and going with the DLR, or one doesn’t exist. Until now. This short post will run you through getting the DLR, building it, and then using it to automate a static C# .NET application (the application is useless, it’s the act of going through the steps that’s meaningful).
The DLR is not part of the .NET 4 release, so you’ll need to download it and build it separately; luckily, that’s very easy to do. Simply go to
http://dlr.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=34834
And download it. Once downloaded, open it up in VS2010 and build it. The binaries you’ll need will be located in a Bin/40 folder at the root of the solution.
Now create a new .NET C# application in VS2010, call it whatever you like. Once made, copy the following binaries to your project folder from Bin/40:
Now reference those from your new solution. To verify everything is bound correctly, add the following code (once you resolve the proper namespace, you shouldn’t get any “red squigglies”):
1: private static ScriptRuntime _runtime = ScriptRuntime.CreateFromConfiguration();
So far, so good. You now have the DLR and Python runtime libraries loaded in your project; you’re now ready to use them.
The purpose of the code which you just added is to bootstrap the scripting runtime’s configuration; and it’s doing so by reading from your application configuration settings. Thus far, however, you haven’t created any configuration settings – so let’s do that now. Add a new App.Config file and add the following:
1: <?xml version="1.0" encoding="utf-8" ?>
2: <configuration>
3: <configSections>
4: <section name="microsoft.scripting" type="Microsoft.Scripting.Hosting.Configuration.Section,
5: Microsoft.Scripting, Version=0.9.6.20, Culture=neutral" requirePermission="false" />
6: </configSections>
7: <microsoft.scripting>
8: <languages>
9: <language names="IronPython;Python;py"
10: extensions=".py"
11: displayName="IronPython 2.6"
12: type="IronPython.Runtime.PythonContext, IronPython, Version=2.6.10920.0,
13: Culture=neutral" />
14: </languages>
15: </microsoft.scripting>
16: </configuration>
In order for scripting languages to interact with your application you’ll need to expose a palette of CLR objects with which it may interact; you do this by creating a ScriptScope and adding CLR objects to it – the ScriptScope acting as the bridge between the two worlds. So let’s do that now. Create a private ScriptScope object as a member variable of your Program class and instantiate it like this:
1: private static ScriptScope _scope = null;
2: static void Main(string[] args)
3: {
4: _scope = _runtime.CreateScope();
5: return;
6: }
Now would be a good time to F5 and verify everything builds and runs correctly.
The next thing to do is to create a class which your scripts may interact against, and you do this by simple creating a POCO:
1: public class MyTest
2: {
3: public string A = "Hello World";
4: }
…and adding it to the scripting scope using SetVariable:
1: static void Main(string[] args)
2: {
3: _scope = _runtime.CreateScope();
4: MyTest test = new MyTest();
5: _scope.SetVariable("MyTest", test);
6: return;
7: }
The last thing to do is to invoke the scripting engine with some IronPython against the object (the IronPython is passed as a string to the Execute() method, so you can imagine loading it from file, from an interactive console, etc.):
1: static void Main(string[] args)
2: {
3: _scope = _runtime.CreateScope();
4: MyTest test = new MyTest();
5: _scope.SetVariable("MyTest", test);
6:
7: ScriptEngine engine = _runtime.GetEngine("IronPython").Execute(
8: "MyTest.A = \"Hello Scripting World\"", _scope);
9:
10: Console.WriteLine(test.A);
11: Console.ReadLine();
12: return;
13: }
The result should be “Hello Scripting World”. There you have it – you now interacted with your program from Python.
Next I’ll go the other way; I’ll execute a method defined in Python from C#.