1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;
using System.IO;
using System.Reflection;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;

namespace TestCallIronPython
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            ScriptEngine pyEngine = null;
            ScriptScope pyScope = null;

            pyEngine = Python.CreateEngine();
            pyScope = pyEngine.CreateScope();
            pyScope.SetVariable("test", "test variable");

            pyEngine.Runtime.LoadAssembly(Assembly.LoadFile(Path.GetFullPath("MyClass.dll")));
            pyScope = pyEngine.ImportModule("MyClass");

            //Invoke a python method
            object pyobject = pyEngine.Operations.Invoke(pyScope.GetVariable("MyClass"));
            pyEngine.Operations.InvokeMember(pyobject, "somemethod", new object[0]);

            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}