# -*- coding: utf-8 -*- # 第一行的目的,是為了讓代碼里面,可以有中文注釋信息. (否則要運行報錯) # 這個 Python 腳本, 用於被 C# 來調用. # 簡單測試 Hello World 的效果. def welcome(name): return "hello " + name # 測試 參數為 C# 對象的效果. (獲取/設置 C# 對象的屬性) def testAddAge(obj): obj.Age = obj.Age + 1 obj.Desc = obj.UserName + "又大了一歲 in Python." # 測試 參數為 C# 對象的效果. (調用 C# 對象的方法) def testAddAge2(obj): obj.AddAge(2) # 測試 List. def testList(lst): vResult = "" for each_item in lst: vResult = vResult + " " + each_item return vResult # 測試 Set. def testSet(pSet): vResult = "" for each_item in pSet: vResult = vResult + " " + each_item return vResult # 測試 Dictionary def testDictionary(pDictionary): vResult = "" for each_item in pDictionary: vResult = vResult + " " + each_item + "=" + pDictionary[each_item] + ";" return vResult
using System; using IronPython.Hosting; using Microsoft.Scripting.Hosting; namespace ConsoleApp1 { /// <summary> /// 測試對象. /// /// 用於傳遞數據給 Python 腳本 /// </summary> public class TestDataObject { /// <summary> /// 用戶名. /// </summary> public string UserName { set; get; } /// <summary> /// 年齡. /// </summary> public int Age { set; get; } /// <summary> /// 描述信息. /// </summary> public string Desc { set; get; } public void AddAge(int age) { this.Age = this.Age + age; this.Desc = String.Format("{0}又大了{1}歲 in C#", this.UserName, age); } public override string ToString() { return String.Format("姓名:{0}; 年齡:{1}; 描述:{2}", this.UserName, this.Age, this.Desc); } } public class RunPython { public void RunPythonTest() { // 加載外部 python 腳本文件. ScriptRuntime pyRumTime = Python.CreateRuntime(); dynamic obj = pyRumTime.UseFile("TestPythonFile.py"); // ================================================== // 簡單調用腳本文件中的方法. Console.WriteLine(obj.welcome("Test C# Call Python.")); Console.WriteLine(obj.welcome("測試中文看看是否正常!")); // ================================================== // 測試自定義對象. TestDataObject testObj = new TestDataObject() { UserName = "張三", Age = 20, Desc = "", }; Console.WriteLine("調用腳本前對象數據:{0}", testObj); obj.testAddAge(testObj); Console.WriteLine("調用 testAddAge 腳本后,對象數據={0}", testObj); obj.testAddAge2(testObj); Console.WriteLine("調用 testAddAge2 腳本后,對象數據={0}", testObj); // ================================================== // 測試 List. IronPython.Runtime.List testList = new IronPython.Runtime.List(); testList.Add("List數據1"); testList.Add("List數據2"); testList.Add("List數據3"); // 測試參數為 List. string result = obj.testList(testList); Console.WriteLine("調用 testList , 返回結果:{0}", result); // ================================================== // 測試 Set. IronPython.Runtime.SetCollection testSet = new IronPython.Runtime.SetCollection(); testSet.add("Set數據1"); testSet.add("Set數據2"); testSet.add("Set數據3"); // 測試參數為 Set. result = obj.testSet(testSet); Console.WriteLine("調用 testSet , 返回結果:{0}", result); // ================================================== // 測試 Dictionary. IronPython.Runtime.PythonDictionary testDictionary = new IronPython.Runtime.PythonDictionary(); testDictionary["Key1"] = "Value1"; testDictionary["Key2"] = "Value2"; testDictionary["Key3"] = "Value3"; // 測試參數為 Dictionary. result = obj.testDictionary(testDictionary); Console.WriteLine("調用 testDictionary , 返回結果:{0}", result); Console.ReadLine(); } } } //-- 運行結果 //hello Test C# Call Python. //hello 測試中文看看是否正常! //調用腳本前對象數據:姓名:張三; 年齡:20; 描述: //調用 testAddAge 腳本后,對象數據=姓名:張三; 年齡:21; 描述:張三又大了一歲 in Py //thon. //調用 testAddAge2 腳本后,對象數據= 姓名:張三; 年齡:23; 描述:張三又大了2歲 in C# //調用 testList , 返回結果: List數據1 List數據2 List數據3 //調用 testSet , 返回結果: Set數據1 Set數據2 Set數據3 //調用 testDictionary , 返回結果: Key3=Value3; Key2=Value2; Key1=Value1;