您当前的位置: 首页 >  Python

彭世瑜

暂无认证

  • 3浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

c#调用python脚本函数_彭世瑜_新浪博客

彭世瑜 发布时间:2017-08-07 17:49:51 ,浏览量:3

需要在Nuget安装IronPython,
如果报错,需要更新Nuget版本则下载对应版本即可:https://dist.nuget.org/index.html
添加引用:IronPython.dll,Microsoft.Scripting.dll(在IronPython的安装目录中)
c#文件:
  1. using IronPython.Hosting;
  2. using Microsoft.Scripting.Hosting;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. namespace PythonDemo
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             ScriptRuntime pyRunTime = Python.CreateRuntime();
  15.             dynamic obj = pyRunTime.UseFile("hello.py");
  16.  
  17.             Console.WriteLine(obj.welcome("Nick"));
  18.             Console.WriteLine(obj.add(1,3));
  19.             Console.ReadKey();
  20.         }
  21.     }
  22. }
python文件:
  1. #hello.py
  2. def welcome(name):
  3.     return "hello " + name
  4.  
  5. def add(a,b):
  6.     return a+b
运行结果:
c#调用python脚本函数
关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.0853s