This walkthrough shows how the new Dynamic feature in C# 4.0
and Visual Basic 10.0 enables the natural use of object models that expect
their host language to provide dynamic dispatch. This capability is
demonstrated for an IronPython scenario of accessing libraries that are written
for the Python language. Note that more advanced IronPython scenarios that
involve hosting your own scripts with access to your application’s object model
are beyond the scope of this walkthrough.
Notes:
- Using Dynamic with Office programming is
demonstrated in the Office Programmability walkthrough.
- Using Dynamic with Silverlight will be shown in
a later preview release of Visual Studio 2010.
Prerequisites:
The IronPython scenario uses the following resources:
IronPython Scenario
This walkthrough demonstrates how to access an IronPython
library from C#.
To set up an IronPython interop project
1. Start
Visual Studio 2010 and create a new C# console application.
2. Add
references to IronPython.dll, IronPython.Modules.dll, and
Microsoft.Scripting.dll (all located in the %PROGRAMFILES%\IronPython 2.6 CTP
for .NET 4.0 Beta 1 folder).
3. Copy the
Python Lib folder into the bin\Debug folder for your project. (for example,
copy the %PROGRAMFILES%\IronPython 2.6 CTP for .NET 4.0 Beta 1\Lib folder into
the C:\IronPythonProject\bin\Debug folder)
4. Add the
following using statements to the
top of the Program.cs/Module1.vb file:
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
Imports IronPython.Hosting
Imports Microsoft.Scripting.Hosting
To import an IronPython module in C#/VB
5. Add code
to create a new ScriptRuntime to
represent the hosted IronPython environment. This ScriptRuntime can then be used to load Python code, in this case
the library module random, which
contains the shuffle function.
Add the following code to the Main method:
Console.WriteLine("Loading random.py...");
ScriptRuntime py = Python.CreateRuntime();
dynamic random = py.UseFile("random.py");
Console.WriteLine("random.py loaded!");
var items = Enumerable.Range(1, 7).ToArray();
Console.WriteLine("Loading random.py...")
Dim py As ScriptRuntime = Python.CreateRuntime()
Dim random As Object = py.UseFile("random.py")
Console.WriteLine("random.py loaded!")
Dim items = Enumerable.Range(1, 7).ToArray()
6. Add the
following code to the end of the Main method, which repeatedly calls the shuffle function to generate random
orderings of the items array:
for (int s = 0; s < 10000; s++)
{
random.shuffle(items);
Console.Write("Sequence {0}: ", s);
foreach (int i in items)
{
Console.Write("{0} ", i);
}
Console.WriteLine();
}
For s As Integer = 1 To 10000
random.shuffle(items)
Console.Write("Sequence {0}: ", s)
For Each i In items
Console.Write("{0} ", i)
Next
Console.WriteLine()
Next
7. Build and
run the application. The Python module random repeatedly shuffles the numbers 1
through 7 in random order 10000 times. Notice that there is a startup cost when
the Python runtime is first initialized, but that each iteration of the loop
then executes extremely fast.