C#使用IronPython调用Python

  using IronPython.Hosting;

  using Microsoft.Scripting.Hosting;

  using System;

  using System.Windows.Forms;

  namespace CSharpCallPython

  {

  public partial class Form1 : Form

  {

  public Form1()

  {

  InitializeComponent();

  }

  private string PythonScriptPath = string.Empty;

  private ScriptEngine PyEngine = null;

  private void Form1_Load(object sender, EventArgs e)

  {

  PythonScriptPath = Application.StartupPath + " est.py";

  PyEngine = Python.CreateEngine();

  }

  private void button1_Click(object sender, EventArgs e)

  {

  dynamic py = PyEngine.ExecuteFile(PythonScriptPath);

  py.sayHi();

  var sum = py.add(3, 5);

  Console.WriteLine("add 方法的和是:{0}", sum);

  int[] arr = new int[3] { 2, 4, 6 };

  var sum1 = py.arrSum(arr);

  Console.WriteLine("数组的和是:{0}", sum1);

  }

  }

  }