[IronPython] How to call a function passing parameters

Christian Schmidt Christian2.Schmidt at gmx.de
Fri Aug 22 23:02:30 CEST 2008


Hi Fernando,

try this:

ScriptEngine engine = ScriptRuntime.Create().GetEngine("py");
ScriptScope scope = engine.CreateScope();
scope.Execute(@"
def test(a):
  return a
");
string my_a = "A";
object func = scope.GetVariable("test");
object result = engine.Operations.Call(func, my_a);
Console.WriteLine(result);

Cheers,
Christian

Fernando Correia schrieb:
> 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
> 

-- 
--
Dr. Christian Schmidt



More information about the Ironpython-users mailing list