一、介紹
Python是一種面向?qū)ο、直譯式計算機程序設計語言,也是一種功能強大而完善的通用型語言,已經(jīng)具有十多年的發(fā)展歷史,成熟且穩(wěn)定。這種語言具有非常簡捷而清晰的語法特點,適合完成各種高層任務,幾乎可以在所有的操作系統(tǒng)中運行。目前,基于這種語言的相關(guān)技術(shù)正在飛速的發(fā)展,用戶數(shù)量急劇擴大,相關(guān)的資源非常多。 IronPython是由Jim Hugunin在微軟領導開發(fā)的一個.NET平臺上的Python實現(xiàn),包括了完整的編譯器、執(zhí)行引擎與運行時支持,能夠與.NET已有的庫無縫整合到一起。微軟對于.NET framework的IronPython和動態(tài)語言非常關(guān)注,已經(jīng)在各種項目中提供了對IronPython的支持。IronPython已經(jīng)很好的集成到了.NET framework中,Python語言中的字符串對應于.NET的字符串對象,并且Python語言中對應的方法,在IronPython中也都提供了。其它數(shù)據(jù)類型也是一樣。
參考:
了解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
創(chuàng)建一個控制臺應用程序,然后到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的結(jié)果值
代碼
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);
執(zhí)行結(jié)果:
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);
執(zhí)行結(jié)果:
Hello:Python
2、C#調(diào)用IronPython 函數(shù)
調(diào)用IronPython 中的MyFunction函數(shù)
代碼
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 調(diào)用C#函數(shù)
在IronPython 中調(diào)用C#中已經(jīng)存在的函數(shù)
代碼
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;
}
如果需要使用某個對象中的某個函數(shù)
代碼
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 實例化使用某個對象,就稍微復雜點,這里我們創(chuàng)建了一個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 *是用來添加命名空間的