Wednesday, April 20, 2011

IronPython and C# - Script Access to C# Objects

Hi all,

Consider the code below:

ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null);
ScriptRuntime runtime = new ScriptRuntime(setup);
ScriptEngine engine = Python.GetEngine(runtime);
ScriptScope scope = engine.CreateScope();

scope.SetVariable("message", "Hello, world!");
string script = @"print message";
ScriptSource source = scope.Engine.CreateScriptSourceFromString(script, SourceCodeKind.Statements);
source.Execute();

This code yields the following exception:

Microsoft.Scripting.Runtime.UnboundNameException was unhandled Message="name 'message' is not defined"

What am I missing?

From stackoverflow

0 comments:

Post a Comment