http://www.cnblogs.com/nuaalfm/archive/2010/02/11/1667448.html

一、介绍

Python是一种面向对象、直译式计算机程序设计语言,也是一种功能强大而完善的通用型语言,已经具有十多年的发展历史,成熟且稳定。这种语言具有非常简捷而清晰的语法特点,适合完成各种高层任务,几乎可以在所有的操作系统中运行。目前,基于这种语言的相关技术正在飞速的发展,用户数量急剧扩大,相关的资源非常多。 IronPython是由Jim Hugunin在微软领导开发的一个.NET平台上的Python实现,包括了完整的编译器、执行引擎与运行时支持,能够与.NET已有的库无缝整合到一起。微软对于.NET framework的IronPython和动态语言非常关注,已经在各种项目中提供了对IronPython的支持。IronPython已经很好的集成到了.NET framework中,Python语言中的字符串对应于.NET的字符串对象,并且Python语言中对应的方法,在IronPython中也都提供了。其它数据类型也是一样。

作者

参考:

了解DLR: http://rednaxelafx.javaeye.com/blog/241430

jim的博客:http://blogs.msdn.com/hugunin/default.aspx

http://developer.51cto.com/art/200910/156377.htm

http://www.msuniversity.edu.cn/m_LearnCenterInfo/Detail.aspx?id=102

二、基础知识

1、安装

因为我目前使用的是vs2008所以到http://ironpython.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=12482下载2.6正式版,我下的是IronPython-2.6.msi ,直接安装就行了。默认的安装路径应该是C:\Program Files\IronPython 2.6

2、引入相应的dll

创建一个控制台应用程序,然后到C:\Program Files\IronPython 2.6中引用IronPython.dll,Microsoft.Scripting.Core.dll,Microsoft.Scripting.dll三个dll。

3、应用

C:\Program Files\IronPython 2.6\Tutorial\Tutorial.htm是IronPython的应用指导,写的很仔细。ipy.exe是IronPython 的运行控制台,如果你想学习IronPython 的语法可以使用这个工具。IronPython 的语法这里就不详细介绍了,如果想进一步学习,可以下载IronPython in Action

三、IronPython 与C#交互

1、C#使用IronPython 代码

我们希望在C#中直接运行IronPython 中的代码,比方说1+2的结果值

ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
var strExpression = "1+2";
var sourceCode = engine.CreateScriptSourceFromString(strExpression);
var actual = sourceCode.Execute<int>();
Console.WriteLine(actual);

执行结果:

3

ScriptEngine和ScriptScope是在.net中使用IronPython 脚本的两个基础类,ScriptSource是运行IronPython 的基础类,这里边sourceCode就是一个ScriptSource。

有时我们希望给IronPython 代码中传入一个变量值

ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
var strExpression = "\"Hello:\" + str";
var sourceCode = engine.CreateScriptSourceFromString(strExpression);
scope.SetVariable("str", "Python");
var actual = sourceCode.Execute<string>(scope);
scope.RemoveVariable("str");
Console.WriteLine(actual);

执行结果:

Hello:Python

2、C#调用IronPython 函数

调用IronPython 中的MyFunction函数

var strExpression = @"
def MyFunction(n):
return 2*n
";
var sourceCode = engine.CreateScriptSourceFromString(strExpression).Compile().Execute(scope);

var func = engine.GetVariable<Func<int, int>>(scope, "MyFunction");

Console.WriteLine(func(3));

这里需要注意def MyFunction(n):前不能有空格,return 2*n 必须有空格

3、IronPython 调用C#函数

在IronPython 中调用C#中已经存在的函数

static void Main(string[] args)
{
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
var strExpression = "CMethod('Python')";
var sourceCode = engine.CreateScriptSourceFromString(strExpression);
scope.SetVariable("CMethod", (Func<string, string>)TMethod);
var actual = sourceCode.Execute<string>(scope);
scope.RemoveVariable("CMethod");
Console.WriteLine(actual);
}
public static string TMethod(string info)
{
return "Hello:" + info;
}

如果需要使用某个对象中的某个函数

 Test test = new Test();
var strExpression = @"
test.Hello()
";
var sourceCode = engine.CreateScriptSourceFromString(strExpression);
scope.SetVariable("test", test);
var actual = sourceCode.Execute<string>(scope);
Console.WriteLine(actual);

如果需要在IronPython 实例化使用某个对象,就稍微复杂点,这里我们创建了一个IronPythonTest程序集,我们希望在IronPython代码中使用IronPythonTest程序集中的Test类,代码如下:

var strExpression = @"
import clr, sys
clr.AddReference('IronPythonTest')
from IronPythonTest import *
test=Test()
test.Hello()
";
var sourceCode = engine.CreateScriptSourceFromString(strExpression);
var actual = sourceCode.Execute<string>(scope);
Console.WriteLine(actual);

Test代码:

namespace IronPythonTest
{
public class Test
{
public string Hello()
{
return "Hello World";
}
}
}

clr.AddReference('IronPythonTest')是用来添加程序集的

from IronPythonTest import *是用来添加命名空间的

参考:

http://www.236z.com/html/30/6/9/2009/11/10/67471.html

IronPython和C#执行速度对比

IronPython 与C#交互的更多相关文章

  1. IronPython和C#交互

    IronPython和C#交互 IronPython是一个.NET平台上的Python实现,包括了完整的编译器.执行引擎与运行时支持,能够与.NET已有的库无缝整合到一起. IronPython已经很 ...

  2. 转Python 和C#的交互

    http://www.cnblogs.com/wilber2013/category/708919.html IronPython和C#交互   IronPython是一个.NET平台上的Python ...

  3. C#Note13:如何在C#中调用python

    前言 IronPython 是一种在 .NET 及 Mono上的 Python 实现,由微软的 Jim Hugunin(同时也是 Jython 创造者) 所发起,是一个开源的项目,基于微软的 DLR ...

  4. C#与Python交互方式

    前言: 在平时工作中,需求有多种实现方式:根据不同的需求可以采用不同的编程语言来实现.发挥各种语言的强项 如:Python的强项是:数据分析.人工智能等 .NET 开发桌面程序界面比Python更简单 ...

  5. 工大助手(C#与python交互)

    工大助手(爬虫--C#与python交互) 基本内容 工大助手(桌面版) 实现登陆.查成绩.计算加权平均分等功能 团队人员 13070046 孙宇辰 13070003 张帆 13070004 崔巍 1 ...

  6. 使用IronPython给.Net程序加点料

    开发的时候,经常被策划频繁变动的方案而苦恼.这时候就想要加入点动态语言来辅助一下. 在考虑用动态语言之前也曾想过使用动态加载dll的方式,实现基础接口来调用.在卸载的时候遇到了问题,虽可以通过应用程序 ...

  7. IronPython脚本调用C#dll示例

    上篇Python脚本调用C#代码数据交互示例(hello world)介绍了与C#紧密结合的示例,这里还将提供一个与C#结合更紧密的示例,直接调用C#编写的DLL.      我们还是沿用了上篇文章的 ...

  8. Python脚本调用C#代码数据交互示例(hello world)

    原地址: http://www.djangochina.cn/forum.php?mod=viewthread&tid=247 随着项目的逐渐收尾, 对IronPython脚本也越来越熟悉,这 ...

  9. IronPython 源码剖析系列(2):IronPython 引擎的运作流程

    http://blog.csdn.net/inelm/article/details/4612987 一.入口点 Python 程序的执行是从 hosting 程序 ipy.exe 开始的,而他的入口 ...

随机推荐

  1. 一段式fsm

    //1-paragraph method to decribe FSM //Describe state transition, state output, state input condition ...

  2. verilog behaviral modeling -- procedural timing contronls

    1.delay control : an expression specifies the time duration between initially encountering the state ...

  3. 在centos7中为php7安装redis扩展

    在此之前一直是用php5.6,安装redis也没遇到啥问题,嗖嗖的就安装上了 更新php版本到php7后,编译的时候报错 include <ext/standard/php_smart_str. ...

  4. 【windows】【php】【nginx】windows 开机自启动nginx php 及nginx php配置

    #启动php-nginx   start-php-nginx.bat   @ECHO OFFECHO Starting PHP FastCGI...RunHiddenConsole.exe php-c ...

  5. Python 基本数据类型 (一) - 整数

    帮助文档网址: https://docs.python.org/3.7/tutorial/introduction.html 待补充

  6. (转)ios应用导航模型

    Eko - MoboCentre 本文将介绍iPhone的导航风格,同时,也一并了解能够组织好应用内容和工具的导航方式.对于一个应用来说,最基础的操作就是基于页面间简单的移动,每张页面都完成一个任务或 ...

  7. ospf 提升 二 ---LSA

    ospf ABR和ASBR的区别 官方建议中大型网络的规模参考   根据spf算法   而不是路由器的硬件性能强弱 a ABR最多关联3个区域 b 单区域内路由器最多50台 c 一台运行ospf的路由 ...

  8. Dialog共通写法(两个button)

    package jp.co.hyakujushibank.view import android.app.Dialogimport android.content.Contextimport andr ...

  9. 【bzoj3671】[Noi2014]随机数生成器 贪心

    题目描述 输入 第1行包含5个整数,依次为 x_0,a,b,c,d ,描述小H采用的随机数生成算法所需的随机种子.第2行包含三个整数 N,M,Q ,表示小H希望生成一个1到 N×M 的排列来填入她 N ...

  10. 【Luogu】P3391文艺平衡树(Splay)

    题目链接 ddosvoid和自为风月马前卒教了我这道题 他们好强啊 如果我们要反转区间[l,r] 我们首先把l的前驱旋转到根节点 再把r的后继旋转到根节点的右儿子 那么此时根节点的右儿子的左儿子所代表 ...