Here's another simple code snippet that'd let you do the same...

using System;
using Microsoft.Scripting.Hosting;
using System.Scripting;


namespace ConsoleApplication1 {
    class Program {

        public static void Main(string[] args) {
            ScriptEngine pyEng = ScriptRuntime.Create().GetEngine("py");
            string testSrc = @"
class FooClass:
     'A simple test class'
     def f(self):return 'Hello World'

fooTest = FooClass()
def bar(): return fooTest.f();";

            // load script with bar() function def
            ScriptSource source = pyEng.CreateScriptSourceFromString(testSrc, 
SourceCodeKind.Statements);
            // Create scope
            ScriptScope scope = pyEng.CreateScope();

            source.Execute(scope);

            object fooTest = scope.GetVariable("fooTest");
            Func<string> sayHello = 
pyEng.Operations.GetMember<Func<string>>(fooTest, "f");

            // Now call fooTest's object member function 'f'
            string result = sayHello();
        }
    }
}


From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Dody Gunawinata
Sent: Saturday, June 28, 2008 7:05 AM
To: Discussion of IronPython
Subject: Re: [IronPython] Call Python function from C#...

Hi Bob,

This blog post shall answer all your questions 
http://spellcoder.com/blogs/dodyg/archive/2008/04/15/12273.aspx

Dody G.
On Fri, Jun 27, 2008 at 5:51 PM, Bob Rosembob <[EMAIL PROTECTED]<mailto:[EMAIL 
PROTECTED]>> wrote:
Hi there,

I apologize if this is a duplicate message. I sent this message last night but 
don't see it posted. Perhaps I did something wrong and message got lost.

I need to call Python function from C# to get a value. When I call the script 
it executes and calculates some value, then I need to call some function to get 
the result. How do I do that? I have the following snippet of code that seems 
to execute the script, but I don't know and can't find on line, how to call a 
function and get value back.

I added references to my project to the following dlls:
- IronPython.dlll
- IronMath.dll

In the code I'm using the following:

using
IronPython.Runtime;

using
IronPython.Runtime.Types;

using
IronPython.Runtime.Operations;

using
IronPython.Hosting;

using
IronPython.Modules;



Here is the code:


PythonEngine engine = new PythonEngine();
EngineModule module = engine.CreateModule("Test", true);
try

{

    engine.ExecuteFile(
@"Test\Test.py", module);

}
catch (Exception ex)

{
    Console.WriteLine("Failed to execute Test.py<http://test.py/> file");
    return;

}



//call the function????



So how do I do the call to the function?



Thanks for your help.

Bob





_______________________________________________
Users mailing list
[email protected]<mailto:[email protected]>
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com



--
nomadlife.org<http://nomadlife.org>
_______________________________________________
Users mailing list
[email protected]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to