c#(IronPython)调用Python方法
直接一段代码演示
public void StartTCP()
{
ScriptEngine engine = Python.CreateEngine();
var paths = engine.GetSearchPaths();
List<string> lstPath = new List<string>();
lstPath.AddRange(paths);
lstPath.Add("Script");
// lstPath.Add(@"D:\Program Files\Python\Python37\Lib");
lstPath.Add(@"D:\Program Files\IronPython 2.7\Lib");
engine.SetSearchPaths(lstPath.ToArray());
var scope = engine.CreateScope();
var source = engine.CreateScriptSourceFromFile("Script/TCPClsClient.py");
dynamic result= source.Execute(scope);
//调用函数的2种方法
// 第一种,通过参数方式转换委托调用,看起来不太简洁
var SetAddress = scope.GetVariable<Action<string,int>>("SetAddress");
var Con = scope.GetVariable<Action>("Connect");
var SendData = scope.GetVariable<Action<string>>("Send");
var Revcive = scope.GetVariable<Action>("Revcive");
var Close = scope.GetVariable<Action>("Close");
SetAddress("localhost", 7777);
Con();
SendData("jinyu");
Revcive();
Close();
//第二种,没有智能化提示,必须要转换为dynamic
result = scope;
result.SetAddress(result, "localhost", 7777);
result.Connect();
result.Send("jinyu");
result.Recvice();
result.Close();
//因为是2.7版本,所以不能支持3.X版本的类方法调用
//例如: result= scope.GetVariable("TCPClsClient");//调用构造,返回实例
//result.SetAddress(result, "localhost", 7777);//不能执行,版本不支持3.X类函数调用
}
c#(IronPython)调用Python方法的更多相关文章
- C#中调用python方法
最近因为项目设计,有部分使用Python脚本,因此代码中需要调用python方法. 1.首先,在c#中调用python必须安装IronPython,在 http://ironpython.codepl ...
- c#利用IronPython调用python的过程种种问题
c#利用IronPython调用python的过程种种问题 小菜鸟一枚,最新学习了Python,感觉语言各种简短,各种第三方类库爽歪歪,毕竟之前是从c#转来的,看到Python的request类各种爽 ...
- odoo14 button 事件调用python方法如何传递参数
1 <field name="user_ids" 2 mode="kanban" 3 nolabel="1" 4 options=&q ...
- VS调用python方法
1. 安装python3.7 2. Vs2010中配置python: 3.添加头文件:#include <Python.h> 4.问题:error LNK2001: 无法解析的外部符号 ...
- 在java中调用python方法
1.http://sourceforge.net/projects/jython/下载jython包,把其中的jython.jar添加到工程目录 示例: 1.摘自:http://blog.csdn.n ...
- C#调用Python 脚本语言
1. 安装IronPython http://pan.baidu.com/s/1qW4jNJ2 下载IronPython 2.7 安装下载下来的安装包 2. 创建项目 创建一个C#的Windows窗 ...
- 在Java中调用Python
写在前面 在微服务架构大行其道的今天,对于将程序进行嵌套调用的做法其实并不可取,甚至显得有些愚蠢.当然,之所以要面对这个问题,或许是因为一些历史原因,或者仅仅是为了简单.恰好我在项目中就遇到了这个问题 ...
- 【.NET调用Python脚本】C#调用python requests类库报错 'module' object has no attribute '_getframe' - IronPython 2.7
最近在开发微信公众号,有一个自定义消息回复的需求 比如用户:麻烦帮我查询一下北京的天气? 系统回复:北京天气,晴,-℃... 这时候需要根据关键字[北京][天气],分词匹配需要执行的操作,然后去调用天 ...
- 在.Net Framework中调用Python的脚本方法 (以VB和C#为例)
某个项目中涉及到这样一个情景: VB/C#写的原始项目要调用Python的一些方法完成特殊的操作, 那么这就涉及到了,在.Net Framework中如何调用Python的脚本方法. 具体步骤流程如下 ...
随机推荐
- 洛谷P1072 Hankson 的趣味题(数学)
题意 题目链接 Sol 充满套路的数学题.. 如果你学过莫比乌斯反演的话不难得到两个等式 \[gcd(\frac{x}{a_1}, \frac{a_0}{a_1}) = 1\] \[gcd(\frac ...
- vue中数组删除,页面没重新渲染
创建一个组件时,数据类型是数组,在删除这个数组中的数据时,数组中的数据是对的,但页面渲染的数据却不对. 举例:(不一定复现) <ul> <li v-for="(item, ...
- puppeteer自动化测试
1.基础知识 puppeteer.launch() 创建浏览器实例 puppeteer.newPage() 创建一个新页面 puppeteer.goto() 进入指定网站 page.screensho ...
- 【Supervised Learning】 集成学习Ensemble Learning & Boosting 算法(python实现)
零. Introduction 1.learn over a subset of data choose the subset uniformally randomly (均匀随机地选择子集) app ...
- Linux制作deb
1.新建一个我们临时的工作目录mkdir deb 2.新建我们程序的目录mkdir hello 3.编写我们的程序 我们以我们最熟悉的helloworld程序做起,hello.c代码如下#includ ...
- 跳过图片反盗链js
页面增加<iframe> <iframe id="ifa" style="display:none" /> 原来html: <im ...
- Linux下Apache HTTP Server 2.4.20安装
一.创建software目录 mkdir /softwareer 二.下载apache源码包 wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4 ...
- selenium+python 数据驱动-txt篇
#循环读取txt文件中的数据,可以作为用户名,密码等使用from selenium import webdriver #创建两个列表user=[]pwd=[]f=open(r'C:\bbs\data\ ...
- 使用redux开发的简单步骤
一.安装redux包 npm install redux --save 二.根据APP数据结构或者后台请求的数据结构拟定state的大致结构. 可以把state写成一个对象字面量,放在reducer文 ...
- hdu-2841 Visible Trees---容斥定理
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2841 题目大意: N*M的格点上有树,从0,0点可以看到多少棵树. 解题思路: 发现如果A1/B1= ...