c#调用python脚本函数_彭世瑜_新浪博客
原创
©著作权归作者所有:来自51CTO博客作者彭世瑜的原创作品,请联系作者获取转载授权,否则将追究法律责任
需要在Nuget安装IronPython,
如果报错,需要更新Nuget版本则下载对应版本即可:https://dist.nuget.org/index.html
添加引用:IronPython.dll,Microsoft.Scripting.dll(在IronPython的安装目录中)
c#文件:
- using IronPython.Hosting;
- using Microsoft.Scripting.Hosting;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace PythonDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- ScriptRuntime pyRunTime = Python.CreateRuntime();
- dynamic obj = pyRunTime.UseFile("hello.py");
-
- Console.WriteLine(obj.welcome("Nick"));
- Console.WriteLine(obj.add(1,3));
- Console.ReadKey();
- }
- }
- }
python文件:
- #hello.py
- def welcome(name):
- return "hello " + name
-
- def add(a,b):
- return a+b
运行结果: