[IronPython] C# and IronPython Interface.

Dino Viehland dinov at exchange.microsoft.com
Tue Nov 27 18:31:51 CET 2007


You're going to want to do something like (only compiled w/ Outlook, so there may be some small compile errors here):

PythonEngine pe = new PythonEngine();
EngineModule mod = pe.CreateModule();
pe.Execute(File.ReadAllText("ArithmeticOperations.py"), mod);
object func = mod.Globals["ArithmeticOperations"];

That gets you the function...  From there you can call it one of several ways:

Object res = Ops.Call(func, a, b);

        Or

delegate object MyDelegate(object a, object b);

MyDelegate dlg = Converter.Convert<MyDelegate>(func);
Object res = dlg(a, b);

In v1.x you can also use the CreateLambda/CreateMethod APIs if you have the body of the code.  In general we prefer the 2nd form more where you convert it to a delegate because it fits in better with .NET.  You can also do things like have the delegate take strongly typed parameters and there's the possibility that could turn into more optimized code in v2.0 at some point in the future.

From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Kaveripakam, Sathish
Sent: Monday, November 26, 2007 8:01 AM
To: users at lists.ironpython.com
Subject: [IronPython] C# and IronPython Interface.

HI All,

I have c#  code which should call a Iron Python function. I am using Iron python 1.1 version and Visual Studio 2005. The details of my files are as follows:


 C# code (Entry.cs):

using System;
using System.Collections;
using IronPython.Hosting;
public class Entry
{
            public static void Main(string[] args)
            {
                        int a= 2, b= 4;
                        // OVER HERE I WOULD LIKE TO CALL IRONPYTHON Function: ArithmeticOperations which takes arguments a,b
                }
}
PYTHON SCRIPT(ArithmeticOperations.py):
def ArithmeticOperations( a,b):
print "I am in ArithmeticOperation functions..."
return  a+b

In my project setup, I gave reference to include : IronPython, IronMath dll's
In this regard, please do let me know, how to call the python script "Arithmetic Operations" from my Entry.cs file ? Also, please do let me know, if I have to include any of the libraries ?
Regards,
Sathish



________________________________________
Legal Notice:
The information in this electronic transmission may contain confidential or legally privileged information and is intended solely for the individual(s) named above. If you are not an intended recipient or an authorized agent, you are hereby notified that reading, distributing, or otherwise disseminating, copying or taking any action based on the contents of this transmission is strictly prohibited. Any unauthorized interception of this transmission is illegal under law. If you have received this transmission in error, please notify the sender by telephone [at the number indicated above/on +41 58 928 0101] as soon as possible and then destroy all copies of this transmission.
________________________________________



More information about the Ironpython-users mailing list