Embedding IronPython in SilverlightUsing IronPython with C#
![]()
Embedding IronPython in SilverlightIronPython code is evaluated and executed at runtime. This opens up all sorts of possibilities for user scripting of Silverlight applications. The IronPython 2 API has been written to make it easy to embed in .NET applications. The basic code looks like: Note This example was built with Visual Studio 2008, using the IronPython binaries supplied as part of the Silverlight SDK and Silverlight tools for Visual Studio. The hosting API for IronPython is still stabilising, and the binaries that come with Dynamic Silverlight are updated (and may have a different API). The Object Browser that is part of Visual Studio 2008 is a great way of exploring these APIs! using IronPython.Hosting; using Microsoft.Scripting; using Microsoft.Scripting.Hosting; ... string code = "lambda x: x.upper()"; Microsoft.Scripting.Hosting.ScriptEngine pe = PythonEngine.CurrentEngine; Microsoft.Scripting.IScriptScope scope = pe.CreateScope(); Microsoft.Scripting.Hosting.SourceUnit source = pe.CreateScriptSourceFromString(code, "id"); Function<string, string> func = pe.Execute<Function<string, string>>(scope, source); string result = func(x);
using System;
using System.Windows.Controls;
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Utils;
namespace EmbeddedIronpython
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
string code = "lambda x: x.upper()";
Microsoft.Scripting.Hosting.ScriptEngine pe = PythonEngine.CurrentEngine;
Microsoft.Scripting.IScriptScope scope = pe.CreateScope();
Microsoft.Scripting.Hosting.SourceUnit source = pe.CreateScriptSourceFromString(code, "id");
Function<string, string> func = pe.Execute<Function<string, string>>(scope, source);
string x = "i'm talking loudly";
string result = func(x);
textblock.Text = result;
}
}
}
There are some other (perhaps more useful) ideas for embedding IronPython 2 in C# applications (using IronPython 2 Beta 1 - the same version used by Dynamic Silverlight) in DLR Hosting: Embedding IronPython. For examples of other ways to use the IronPython 2 API, check out the IronPython Cookbook. The final article in this series is about using Silverlight to put an interactive Python interpreter in the browser: For buying techie books, science fiction, computer hardware or the latest gadgets: visit The Voidspace Amazon Store. If you're looking for a new techie job, try the Voidspace Tech Job Board. This is part of the Hidden Network of technology and programming jobs.
Last edited Mon Apr 21 00:56:15 2008. Counter... |
|
|
Blogads
Follow me on: Tech Jobs |