若是須要查看更多文章,請微信搜索公衆號 csharp編程大全,須要進C#交流羣羣請加微信z438679770,備註進羣, 我邀請你進羣! ! !python
爲了記錄本身的學習成果,防止遺忘,同時爲了幫助在C#學習過程當中遇到一樣問題的同志們,因此將原版代碼奉上!!!編程
開發環境:vs2015微信
準備工做:函數
下載並安裝Ironpython學習
建立控制檯應用程序spa
添加文本文件,並命名爲test.py對象
添加引用:引用IronPython.dll,Microsoft.Scripting.dll 兩個dll文件blog
控制檯程序代碼:ip
using System;開發
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//引用須要的命名空間
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
/*執行bin目錄下test.py*/
/*ScriptEngine pyEngine = Python.CreateEngine();//建立Python解釋器對象
dynamic py = pyEngine.ExecuteFile(@"test.py");//讀取腳本文件
int[] array = new int[9] { 9, 3, 5, 7, 2, 1, 3, 6, 8 };
string reStr = py.main(array);//調用腳本文件中對應的函數
Console.WriteLine(reStr);
Console.ReadKey();*/
ScriptRuntime pyRunTime = Python.CreateRuntime();
dynamic obj = pyRunTime.UseFile("test.py");
Console.WriteLine(obj.welcome(10));
Console.ReadKey();
}
}
}
python程序:
import math
def welcome(value):
return math.pi + value
#print(welcome(100))
運行前注意:點擊test.py 將複製到輸出目錄 設置爲始終複製
運行:直接按F5
執行效果: