[IronPython] Calling a Python function (compiled into an assembly) from C# using delegates.

Michael Foord fuzzyman at voidspace.org.uk
Wed Jan 21 17:08:51 CET 2009


Renaud Durand wrote:
> Ok, but how ?
In the code you showed you create an empty scope and then attempt to 
fech a variable from it - something doomed to failure...

Where does the assembly 'interpreter' come from?

If it is compiled IronPython code then the filename will correspond to a 
module name (the namespace) - so if you execute 'from module import 
interpretMapping' in the scope you have created, you will then be able 
to fetch the module.

If it is compiled IronPython code then you may need to do something else 
other than load it with the LoadAssembly API. Can't remember off the top 
of my head what the API to use is?

Something like the following should work:

    eng.GetClrModule().AddReference(interpreter);

Michael
>
> 2009/1/21 Dody Gunawinata <empirebuilder at gmail.com 
> <mailto:empirebuilder at gmail.com>>
>
>     I think you have to import the function - loading the assembly
>     alone is not enough.
>
>     On Wed, Jan 21, 2009 at 5:17 PM, Renaud Durand <renaud.durand.it
>     <http://renaud.durand.it>@gmail.com <http://gmail.com>> wrote:
>
>         Hi again,
>
>         I'm trying to call a python function from C#. I found out
>         through a tutorial how to do it from
>         a Python source file but I would like to do it from a compiled
>         assembly. Some elements are missing.
>         I think it is something like below but this does not work:
>
>                     eng = Python.CreateEngine()
>                     Assembly interpreter = Assembly.Load("interpreter");
>                     eng.runtime.LoadAssembly(interpreter);
>               
>                     ScriptScope scope = eng.CreateScope();
>
>                     //Get the interpretMapping function
>                     Func<string, object> interpretMapping;
>                     interpretMapping = scope.GetVariable<Func<string,
>         object>>("interpretMapping");
>                     ObjectOperations ops = eng.Operations;
>
>                     //Get the interpItem returned by function.
>                     object item =
>         interpretMapping("649AC0165011B1E8F726AC54C911000000000000000000000000000000000000");
>                     //Get the display Method
>                     object method = ops.GetMember(item, "display");
>                     ops.Call(method);
>
>         Thank you... Again :-)
>
>         -- 
>         Renaud Durand
>         EPITA Student
>
>         _______________________________________________
>         Users mailing list
>         Users at lists.ironpython.com <mailto:Users at lists.ironpython.com>
>         http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
>
>
>     -- 
>     nomadlife.org <http://nomadlife.org>
>
>
>     _______________________________________________
>     Users mailing list
>     Users at lists.ironpython.com <mailto:Users at lists.ironpython.com>
>     http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
>
>
> -- 
> Renaud Durand
> ------------------------------------------------------------------------
>
> _______________________________________________
> 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