IronPython 与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的結果值
代碼
ScriptEngineengine=Python.CreateEngine();
ScriptScopescope=engine.CreateScope();
varstrExpression="1+2";
varsourceCode=engine.CreateScriptSourceFromString(strExpression);
varactual=sourceCode.Execute<int>();
Console.WriteLine(actual);
執行結果:
3
ScriptEngine和ScriptScope是在.net中使用IronPython 腳本的兩個基礎類,ScriptSource是運行IronPython 的基礎類,這里邊sourceCode就是一個ScriptSource。
有時我們希望給IronPython 代碼中傳入一個變量值
代碼
ScriptEngineengine=Python.CreateEngine();
ScriptScopescope=engine.CreateScope();
varstrExpression="\"Hello:\"+str";
varsourceCode=engine.CreateScriptSourceFromString(strExpression);
scope.SetVariable("str","Python");
varactual=sourceCode.Execute<string>(scope);
scope.RemoveVariable("str");
Console.WriteLine(actual);
執行結果:
Hello:Python
2、C#調用IronPython 函數
調用IronPython 中的MyFunction函數
代碼
varstrExpression=@"
defMyFunction(n):
return2*n
";
varsourceCode=engine.CreateScriptSourceFromString(strExpression).Compile().Execute(scope);
varfunc=engine.GetVariable<Func<int,int>>(scope,"MyFunction");
Console.WriteLine(func(3));
這里需要注意def MyFunction(n):前不能有空格,return 2*n 必須有空格
3、IronPython 調用C#函數
在IronPython 中調用C#中已經存在的函數
代碼
staticvoidMain(string[]args)
{
ScriptEngineengine=Python.CreateEngine();
ScriptScopescope=engine.CreateScope();
varstrExpression="CMethod('Python')";
varsourceCode=engine.CreateScriptSourceFromString(strExpression);
scope.SetVariable("CMethod",(Func<string,string>)TMethod);
varactual=sourceCode.Execute<string>(scope);
scope.RemoveVariable("CMethod");
Console.WriteLine(actual);
}
publicstaticstringTMethod(stringinfo)
{
return"Hello:"+info;
}
如果需要使用某個對象中的某個函數
代碼
Testtest=newTest();
varstrExpression=@"
test.Hello()
";
varsourceCode=engine.CreateScriptSourceFromString(strExpression);
scope.SetVariable("test",test);
varactual=sourceCode.Execute<string>(scope);
Console.WriteLine(actual);
如果需要在IronPython 實例化使用某個對象,就稍微復雜點,這里我們創建了一個IronPythonTest程序集,我們希望在IronPython代碼中使用IronPythonTest程序集中的Test類,代碼如下:
代碼
varstrExpression=@"
importclr,sys
clr.AddReference('IronPythonTest')
fromIronPythonTestimport*
test=Test()
test.Hello()
";
varsourceCode=engine.CreateScriptSourceFromString(strExpression);
varactual=sourceCode.Execute<string>(scope);
Console.WriteLine(actual);
Test代碼:
namespaceIronPythonTest
{
publicclassTest
{
publicstringHello()
{
return"HelloWorld";
}
}
}
clr.AddReference('IronPythonTest')是用來添加程序集的
from IronPythonTest import *是用來添加命名空間的
參考:
http://www.236z.com/html/30/6/9/2009/11/10/67471.html
IronPython和C#執行速度對比
作者:Lance
出處:http://www.cnblogs.com/nuaalfm/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
總結
以上是生活随笔為你收集整理的IronPython 与C#交互的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (python)图片处理Pillow模块
- 下一篇: 怎么创建具有真实纹理的CG场景岩石?