这篇文章对Python和c#交互做了简单介绍,用法比较简单点击查看原文

一、介绍
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 *是用来添加命名空间的


优克斯股票采用IronPython作为脚本引擎,能够简单的在python中调用.NET对象实现内置的绘图功能。


更多相关文章

  1. 支持c和python之间的跨语言(c)标记的代码编辑器
  2. 运用Python语言编写获取Linux基本系统信息(三):Python与数据库编
  3. 【小白自学笔记】【机器学习实战】【Python代码逐行理解】CH02
  4. 三个猜数字游戏代码(Python)
  5. 在混合的Bash-Python代码片段中,变量的双引号和单引号
  6. 《数据结构与算法Python语言描述》裘宗燕 笔记 第五章 栈和队列
  7. [Z] 通天塔导游:各种编程语言的优缺点
  8. 八大经典排序算法基本思想及代码实现(插入排序,希尔排序,选择排序,
  9. 贝叶斯学习 -- matlab、python代码分析(3)

随机推荐

  1. java实现能计算10道基本运算的计算器
  2. 黑马程序员_Java基础_异常
  3. Java反射---getGenericSuperclass和Param
  4. java中循环遍历删除List和Set集合中元素
  5. getter on xmlbeans生成的类返回null,它不
  6. 如何从webview获取javascript值到android
  7. 当只使用get()和set()方法时,用原始类型替换At
  8. 蓝桥杯 ALGO-53 算法训练 最小乘积(基本
  9. 关于LSA的相关学习---短文本聚类涉及到的
  10. 理顺 JavaScript (12) - 一个比较实用的