为了记录自己的学习成果,防止遗忘,同时为了帮助在C#学习过程中遇到同样问题的同志们,所以将原版代码奉上!!! 开发环境:vs2015 准备工作:
控制台程序代码: 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 执行效果: /*对于以上代码,亲测无误,如有任何问题可以在平台下方留言或者直接加我微信z438679770*/ |
|
来自: ontheroad96j47 > 《待分类》