[IronPython] Access functions from other modules in an embedded environment

Danny Fernandez fernandez.dan at gmail.com
Mon Jul 12 23:51:08 CEST 2010


Hi I am planning on using IronPython 2.6.1 .NET 2.0 in a embedded in my C#
app. I am playing with able
to call python functions from C#. I had a question about how do you call
other functions from modules
that were imported. Here is a simple example

moduleA.py has the following

import moduleB

def myFunA():
 return 1


moduleB.py has the following

def myFunB():
 return 2

This is what I have in my sample C# program.

ScriptSource source = _engine.CreateScriptSourceFromFile("moduleA);
CompiledCode compiledCode = source.Compile();
ScriptScope scope = _engine.CreateScope();
compiledCode.Execute(scope);


I can get access to myFunA straight forward with

Func<int> funcA = scope.GetVariable<Func<int>>("myFunA");


How do I access other functions from other modules in this simple case
myFunB from the import moduleB?
I was able to with the following but I am not sure if this is correct way or
if there are better way

PythonModule stuff = scope.GetVariable<PythonModule>("moduleB");
Func<int> funcB= _engine.Operations.GetMember<Func<int>>(stuff, "myFunB");


Thanks

Danny
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20100712/2a60fd37/attachment.html>


More information about the Ironpython-users mailing list