IronPythonのインストールディレクトリにあるIronPython.dll, Microsoft.Scripting.dll, Microsoft.Scripting.Core.dll, Nicrosoft.Dynamic.dllに参照を通す。
下記のソースコードのような方法でC#のコードからPythonのスクリプトの実行と変数の値のやりとりが可能。
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
public class Class1
{
public IronPython.Runtime.List Val1;
public IronPython.Runtime.List Val2;
}
class Program
{
static void Main(string[] args)
{
ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromString(
@”print obj1.Val1
result = obj1.Val2″);
ScriptScope scope = engine.CreateScope();
var obj1 = new Class1()
{
Val1 = new IronPython.Runtime.List { 1, “abc” },
Val2 = new IronPython.Runtime.List { 2, “def”}
};
scope.SetVariable(“obj1″, obj1);
source.Execute(scope);
object[] result = scope.GetVariable<IronPython.Runtime.List>(“result”).ToArray();
Console.WriteLine(“{0},{1}”, result[0], result[1]);
}
}