[IronPython] How to call a function passing parameters

Tomas Matousek Tomas.Matousek at microsoft.com
Fri Aug 22 22:41:35 CEST 2008


You can do:

var runtime = ScriptRuntime.Create();
var scope = runtime.CreateScope("python");

scope.Execute(@"
def test(a):
  return a
");

var test = scope.GetVariable<Func<string, string>>("test");

Console.WriteLine(test(my_a));

Tomas

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Fernando Correia
Sent: Friday, August 22, 2008 1:32 PM
To: Discussion of IronPython
Subject: [IronPython] How to call a function passing parameters

Please help me to call an IronPython function passing parameters from
a C# method.

Let's say I have a ScriptEngine and also a ScriptScope. In the scope I
have a Python function like this:

def test(a):
    return a

Then in some C# method I have something like:

string my_a = "A";

How can I call that function passing to it my_a in place of the "a"
parameter? I want to pass the actual object, not just its value, so I
can pass complex objects and manipulate them in IronPython.

Without passing parameters, I can already do this:

var functionCall = string.Format("{0}()", FunctionName);
ScriptSource function =
engine.CreateScriptSourceFromString(functionCall,
SourceCodeKind.Expression);
function.Execute(scope);

Thanks for any help.
_______________________________________________
Users mailing list
Users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com




More information about the Ironpython-users mailing list