如果需要查看更多文章,請微信搜索公眾號 csharp編程大全,需要進C#交流群群請加微信z438679770,備注進群, 我邀請你進群! ! !

為了記錄自己的學習成果,防止遺忘,同時為了幫助在C#學習程序中遇到同樣問題的同志們,所以將原版代碼奉上!!!
開發環境:vs2015
準備作業:
-
下載并安裝Ironpython
-
創建控制臺應用程式
-
添加文本檔案,并命名為test.py
-
添加參考:參考IronPython.dll,Microsoft.Scripting.dll 兩個dll檔案
控制臺程式代碼:
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
執行效果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/195853.html
標籤:.NET技术
上一篇:ASP.NET(C#)操作SQL Server資料庫
下一篇:C#資料庫操作類
