none
C# + IronPython + Reflection RRS feed

  • Question

  • Sorry, my english is poor. 

    I have a problem. I want to execute python function from script, but name of function is argument. I have written code:

    var engine = Python.CreateEngine();
    dynamic py = engine.ExecuteFile("calc.py");
    
    dynamic calc = py.Calculator();
    Type myTypeObj = calc.GetType();
    // Name of function
    MethodInfo myMethodInfo = myTypeObj.GetMethod("add");
    object[] mParam = new object[] { 5, 10 };
    Console.Write(myMethodInfo.Invoke(calc, mParam) + "\n");
    


    Unfortunately , this code not working. Why?

    Saturday, January 28, 2012 4:18 PM

Answers

  • Standard reflection doesn't work on dynamic objects. To call members by name, use the DLR's operations instead:

    var engine = Python.CreateEngine();
    dynamic py = engine.ExecuteFile("calc.py");
    
    dynamic calc = py.Calculator();
    dynamic result = engine.Operations.InvokeMember(calc, "add", 5, 10);
    Console.WriteLine(result);
    

     


    • Marked as answer by siszarp Monday, January 30, 2012 6:06 PM
    • Edited by JeffHardy Monday, January 30, 2012 7:59 PM
    Monday, January 30, 2012 4:26 PM
  • Correct code:

     var engine = Python.CreateEngine();
     dynamic py = engine.ExecuteFile(@"calc.py");
     dynamic calc = py.Calculator();
    dynamic result = engine.Operations.InvokeMember(calc,"add", new object[] { 5, 10 });
    Console.WriteLine(result);
    Console.Read();
    
    I am very grateful for your help.

    Monday, January 30, 2012 6:08 PM

All replies

  • You created your python as dll?

    What is the error message exactly?

    chanmm


    chanmm
    Monday, January 30, 2012 1:02 AM
  • Standard reflection doesn't work on dynamic objects. To call members by name, use the DLR's operations instead:

    var engine = Python.CreateEngine();
    dynamic py = engine.ExecuteFile("calc.py");
    
    dynamic calc = py.Calculator();
    dynamic result = engine.Operations.InvokeMember(calc, "add", 5, 10);
    Console.WriteLine(result);
    

     


    • Marked as answer by siszarp Monday, January 30, 2012 6:06 PM
    • Edited by JeffHardy Monday, January 30, 2012 7:59 PM
    Monday, January 30, 2012 4:26 PM
  • Correct code:

     var engine = Python.CreateEngine();
     dynamic py = engine.ExecuteFile(@"calc.py");
     dynamic calc = py.Calculator();
    dynamic result = engine.Operations.InvokeMember(calc,"add", new object[] { 5, 10 });
    Console.WriteLine(result);
    Console.Read();
    
    I am very grateful for your help.

    Monday, January 30, 2012 6:08 PM
  • Thanks Jeff and chanmm for your help.

    Hi siszarp,

    Welcome to MSDN Forum.

     

    We are very glad to hear that you got the solution here. Thanks for your share. It will be very beneficial for other community members who have the similar questions.

    If you have any difficulty in future programming, you are welcome to post here again.


    Martin Xie [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, January 31, 2012 3:03 AM
    Moderator