python與C#的互相調用
一、C#調用python
新建一個項目,添加引用:IronPython.dll,Microsoft.Scripting.dll(在IronPython的安裝目錄中)。
創建一個文本文件命名為hello.py,把該文件添加的當前的項目中,并設置為總是輸出。
#hello.py
def welcome(name):
? ? return "hello" + name
調用hello.py文件中的方法:
static void main(string[] args)
{
? ? ScriptRuntime pyRunTime=Python.CreateRuntime();
? ? dynamic obj=pyRunTime.UseFile("hello.py");
? ? Console.Write(obj.welcome("Nick"));
? ? Console.ReadKey();
}
二、Python調用C#
示例一:調用dll中的方法
1.先準備一個C#寫的dll,名稱為IronPython_TestDll.dll
using System;
using System.Collections.Generic;
using System.Text;
namespace IronPython_TestDll
{
? ? public ?class TestDll
? ? {
? ? ? ? public static int Add(int x, int y)
? ? ? ? {
? ? ? ? ? ? return x + y;
? ? ? ? }
? ? }
? ? public class TestDll1
? ? {
? ? ? ? private int aaa = 11;
? ? ? ? public int AAA
? ? ? ? {
? ? ? ? ? ? get { return aaa; }
? ? ? ? ? ? set { aaa = value; }
? ? ? ? }
? ? ? ? public void ShowAAA()
? ? ? ? {
? ? ? ? ? ? global::System.Windows.Forms.MessageBox.Show(aaa.ToString());
? ? ? ? }
? ? }
}
2.調用C#的dll中的方法
import clr
clr.AddReferenceByPartialName("System.Windows.Forms")
clr.AddReferenceByPartialName("System.Drawing")
from System.Windows.Forms import *
from System.Drawing import *
clr.AddReferenceToFile("IronPython_TestDll.dll")
from IronPython_TestDll import *
a=12
b=6
#靜態方法可以直接調用
c=TestDll.Add(a,b)
MessageBox.Show(c.ToString())
#普通方法需要先定義類
td=TestDll1()
td.AAA=100
td.ShowAAA()
示例二:動態執行python代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using IronPython.Hosting;
namespace TestIronPython
{
? ? public partial class Form1 : Form
? ? {
? ? ? ? public Form1()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? }
? ? ? ? private void button1_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? PythonEngine scriptEngine = new PythonEngine();
? ? ? ? //設定dll文件所在的目錄
? ? ? ? ? ? scriptEngine.AddToPath(Application.StartupPath);?
? ? ? ? //textBox1.Text中寫的是python代碼,但調用的是dll中的方法
? ? ? ? ? ? scriptEngine.Execute(textBox1.Text); ? ? ? ? ? ? ? ?
? ? ? ? }
? ? }
}
//textBox1.Text中寫的是如下代碼,會計算彈出100的提示框。
a=12
b=6
c=TestDll.Add(a,b)
MessageBox.Show(c.ToString())
td=TestDll1()
td.AAA=100
td.ShowAAA()
一、C#調用python
新建一個項目,添加引用:IronPython.dll,Microsoft.Scripting.dll(在IronPython的安裝目錄中)。
創建一個文本文件命名為hello.py,把該文件添加的當前的項目中,并設置為總是輸出。
#hello.py
def welcome(name):
? ? return "hello" + name
調用hello.py文件中的方法:
static void main(string[] args)
{
? ? ScriptRuntime pyRunTime=Python.CreateRuntime();
? ? dynamic obj=pyRunTime.UseFile("hello.py");
? ? Console.Write(obj.welcome("Nick"));
? ? Console.ReadKey();
}
二、Python調用C#
示例一:調用dll中的方法
1.先準備一個C#寫的dll,名稱為IronPython_TestDll.dll
using System;
using System.Collections.Generic;
using System.Text;
namespace IronPython_TestDll
{
? ? public ?class TestDll
? ? {
? ? ? ? public static int Add(int x, int y)
? ? ? ? {
? ? ? ? ? ? return x + y;
? ? ? ? }
? ? }
? ? public class TestDll1
? ? {
? ? ? ? private int aaa = 11;
? ? ? ? public int AAA
? ? ? ? {
? ? ? ? ? ? get { return aaa; }
? ? ? ? ? ? set { aaa = value; }
? ? ? ? }
? ? ? ? public void ShowAAA()
? ? ? ? {
? ? ? ? ? ? global::System.Windows.Forms.MessageBox.Show(aaa.ToString());
? ? ? ? }
? ? }
}
2.調用C#的dll中的方法
import clr
clr.AddReferenceByPartialName("System.Windows.Forms")
clr.AddReferenceByPartialName("System.Drawing")
from System.Windows.Forms import *
from System.Drawing import *
clr.AddReferenceToFile("IronPython_TestDll.dll")
from IronPython_TestDll import *
a=12
b=6
#靜態方法可以直接調用
c=TestDll.Add(a,b)
MessageBox.Show(c.ToString())
#普通方法需要先定義類
td=TestDll1()
td.AAA=100
td.ShowAAA()
示例二:動態執行python代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using IronPython.Hosting;
namespace TestIronPython
{
? ? public partial class Form1 : Form
? ? {
? ? ? ? public Form1()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? }
? ? ? ? private void button1_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? PythonEngine scriptEngine = new PythonEngine();
? ? ? ? //設定dll文件所在的目錄
? ? ? ? ? ? scriptEngine.AddToPath(Application.StartupPath);?
? ? ? ? //textBox1.Text中寫的是python代碼,但調用的是dll中的方法
? ? ? ? ? ? scriptEngine.Execute(textBox1.Text); ? ? ? ? ? ? ? ?
? ? ? ? }
? ? }
}
//textBox1.Text中寫的是如下代碼,會計算彈出100的提示框。
a=12
b=6
c=TestDll.Add(a,b)
MessageBox.Show(c.ToString())
td=TestDll1()
td.AAA=100
td.ShowAAA()