IronPython C#与Python相互调用
ironphy microsoft.scripting dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using Common;
namespace PyConsoleTest
{
class Program
{
static void Main(string[] args)
{
//ScriptRuntime pyRuntime = Python.CreateRuntime();
//dynamic py = pyRuntime.UseFile(@"E:\Test\test.py");
var engine = Python.CreateEngine();
var scope = engine.CreateScope();
var source = engine.CreateScriptSourceFromFile(@"E:\Test\test.py");
source.Execute(scope);
var say_hello = scope.GetVariable<Func<object>>("say_hello");
say_hello();
var get_text = scope.GetVariable<Func<object>>("get_text");
var text = get_text().ToString();
Console.WriteLine(text);
var add = scope.GetVariable<Func<object, object,object>>("add");
var name = scope.GetVariable<Func<object,object>>("get_name");
int[] ints = { 1, 2, 3, 4 };
List<int> list = new List<int>();
Dictionary<int, int> dic = new Dictionary<int, int>();
int i = 0;
SimpleTest test = new SimpleTest();
var result1 = name(test); Python调用C#
list.AddRange(ints);
var result2 = add(list, i); 传递参数并返回值
Console.WriteLine(result2);
//var result1 = add("hello", i.ToString());
//Console.WriteLine(result1);
Console.Read();
}
}
}
以下C#代码测试Python调用C#时使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Common
{
public class SimpleTest
{
public string GetName()
{
return "C#";
}
}
}
Python代码
import clr
import sys
sys.path.append(r'E:\Test\PyConsoleTest\PyConsoleTest\bin\Debug')
clr.AddReferenceToFile("Common.dll")
from Common import *
def say_hello():
print "hello!"
def get_text():
return "text from hello.py"
def add(arg1,arg2): 传递参数返回参数
#var y
for x in arg1:
arg2 += x
return arg2
#print y
#return arg1+arg2;
def get_name(SimpleTest): 调用方法返回
return SimpleTest.GetName()
PS:如果你的Python没有设置encoding 删除所有中文即可使用
IronPython C#与Python相互调用的更多相关文章
- c/c++再学习:C与Python相互调用
c/c++再学习:Python调用C函数 Python 调用C函数比较简单 这里两个例子,一个是直接调用参数,另一个是调用结构体 C代码 typedef struct { int i1; int i2 ...
- Python实例浅谈之三Python与C/C++相互调用
一.问题 Python模块和C/C++的动态库间相互调用在实际的应用中会有所涉及,在此作一总结. 二.Python调用C/C++ 1.Python调用C动态链接库 Python调用C库比较简单,不经过 ...
- python调用C++之pybind11入门(相互调用)
python调用C/C++有不少的方法,如boost.python, swig, ctypes, pybind11等,这些方法有繁有简,而pybind11的优点是对C++ 11支持很好,API比较简单 ...
- python - 函数的相互调用 及 变量的作用域
# -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: study_函数的相互调用及变量的作用域.py@ide: PyCharm C ...
- Python与C/C++相互调用(python2 调c++那个试了ok)
一.问题 Python模块和C/C++的动态库间相互调用在实际的应用中会有所涉及,在此作一总结. 二.Python调用C/C++ 1.Python调用C动态链接库 Python调用C库比较简单,不经过 ...
- Python与C/C++相互调用(转)
原文链接 作者 一.问题 Python模块和C/C++的动态库间相互调用在实际的应用中会有所涉及,在此作一总结. 二.Python调用C/C++ 1.Python调用C动态链接库 Python调用C库 ...
- python模块--如何相互调用自己写的模块
一.模块相互调用同级目录调用时的两种方法 import module print(module.add(3,8)) from module import add print(add(2,4)) 同级目 ...
- Python与Javascript相互调用超详细讲解(2022年1月最新)(三)基本原理Part 3 - 通过C/C++联通
目录 TL; DR python调javascript javascript调python 原理 基于Node.js的javascript调用python 从Node调用python函数 V8 嵌入P ...
- 各编程语言 + aardio 相互调用示例
代码简单.复制可用.aardio 快速调用 C,C++,C#,Java,R,V,Python,JavaScript,Node.js,Rust,PHP,Ruby,PowerShell,Fortran,D ...
随机推荐
- C/C++ C++ 11 std::function和std::bind用法
std::bind() std::bind 主要用于绑定生成目标函数,一般用于生成的回调函数,cocos的回退函数都是通过std::bind和std::function实现的.两个点要明白: 1.绑定 ...
- MaxCompute studio FAQ
1. 官方文档地址 https://help.aliyun.com/document_detail/50889.html 2. Show Table Detail 中文乱码 原因是Intellij A ...
- router登录逻辑实现页面跳转
main.js文件中router.beforeEach((to, from, next) => { NProgress.start() const token = localStorage.ge ...
- AngularJS ng-model 指令
AngularJS ng-model 指令 ng-model 指令用于绑定应用程序数据到 HTML 控制器(input, select, textarea)的值. 代码示例如下: <!DOCTY ...
- css3D动画
css3D动画 前言 最近玩了玩用css来构建3D效果,写了几个demo,所以博客总结一下. 在阅读这篇博客之前,请先自行了解一下css 3D的属性,例如:transform-style,transf ...
- vue.js使用echarts一分钟简单入门
图表的使用在企业级软件中使用越来越普遍,前端开发人员可以使用常用的echarts开源库来进行图表展示的开发,公司最近提出需要丰富系统首页的内容,趁此机会分享一下如何在使用vue.js框架下使用echa ...
- python编写计算器
程序代码 # coding: utf-8# 将tkinter改为Tkinter兼容Python 2.xfrom tkinter import *class App: def __init__(self ...
- Where should I put <script> tags in HTML markup?
Where should I put <script> tags in HTML markup? When embedding JavaScript in an HTML document ...
- 20150721—HTML的定位 JS (转)
本文转载于:http://blog.csdn.net/xuantian868/article/details/3116442 HTML:scrollLeft,scrollWidth,clientW ...
- CentOS7下python虚拟环境
搭建python虚拟环境 1.我们先创建一个隐藏目录 .virtualenvs,所有的虚拟环境都放在此目录下 :mkdir /root/.virtualenvs 2.安装虚拟环境 确认pip:wher ...