ironpython 2.0 beta 5 已经发布,下载地址:http://www.codeplex.com/IronPython/Release/ProjectReleases.aspx?ReleaseId=15625。IronPython 2.0 Beta 5是2.0系列的最后一个beta版本,下个版本就是RC版了。也就是说到了下个版本所有API都将固化了。现在也是到学习IronPython 2的时间了。从beta 4发布就有了msi 安装文件,并且带来了python的标准库 ,标准库的许可是以Python Software Foundation license 发布,这也就意味着移值cpython应用会很容易了。

ironpython 2.0 beta 5 已经发布,下载地址:​​http://www.codeplex.com/IronPython/Release/ProjectReleases.aspx?ReleaseId=15625​​。IronPython 2.0 Beta 5是2.0系列的最后一个beta版本,下个版本就是RC版了。也就是说到了下个版本所有API都将固化了。现在也是到学习IronPython 2的时间了。从beta 4发布就有了msi 安装文件,并且带来了python的标准库 ,标准库的许可是以Python Software Foundation license 发布,这也就意味着移值cpython应用会很容易了。

值得注意的是带来了DLR hosting spec 的稳定版本,你可以从这里下载 DLR hosting spec的word 版本 ​​http://compilerlab.members.winisp.net/dlr-spec-hosting.doc​

这个版本还有一个最明显的变化的是命名空间作了个重大的修改,把所有的DLR 类型从System移到了Microsoft,原因是​​http://lists.ironpython.com/pipermail/users-ironpython.com/2008-August/thread.html#8036​​ 许多人把IronPython嵌入到C# (.NET 3.5)项目中。

另外一点是DLR 宿主API不有一个针对Python的默认配置,现在应该用IronPython.Hosting.Python去创建脚本引擎,这样在应用程序中宿主IronPython就更容易了。IronPython.Hosting.Python有几个辅助方法,以创建一个ScriptRuntime或ScriptEngine ,并为ScriptRuntime和ScriptEngine增加了一些Python-specific 扩展方法。

using IronPython.Hosting;

ScriptEngine engine = Python.CreateEngine();

ScriptScope sys = engine.GetSysModule();
var platform = sys.GetVariable("platform");
Console.WriteLine(platform);

ScriptScope builtins = engine.GetBuiltinModule();
var pow = builtins.GetVariable<Func<double, double,double>>("pow");
Console.WriteLine(pow(2,3));

ScriptScope clr = engine.GetClrModule();
var getPythonType = clr.GetVariable<Func<Type, PythonType>>("GetPythonType");           
Console.WriteLine(PythonType.Get__name__(getPythonType(typeof(string))));


如何在托管语言中调用动态语言。

首先,我们需要初始化动态语言的环境配置,从中获取所有可以使用的动态语言列表,然后得到相应动态语言的运行引擎。

ScriptRuntimeSetup setup = new ScriptRuntimeSetup(true); //true表示载入所有支持的动态语言的环境配置
ScriptRuntime runtime = ScriptRuntime.Create(setup); //创建动态语言运行环境

foreach (LanguageProviderSetup langSetup in setup.LanguageProviders) //遍历所有动态语言的环境配置

{

try

{

ScriptEngine engine = null;

if (runtime.TryGetEngine(langSetup.Names[0], out engine)) //尝试获取动态语言的运行引擎

{

//engine就是我们需要的运行引擎

}

}

catch (MissingTypeException) //处理创建不支持的动态语言时可能抛出异常

{

}

}获取了运行引擎后,我们就可以执行动态语言的代码了
public class MyErrorSink : ErrorSink //编译错误处理

{

IList<string> m_ErrorMsg = new List<string>();

public IList<string> ErrorMsg

{

get { return m_ErrorMsg; }

}


public MyErrorSink()

{

}

public virtual void Add(SourceUnit source, string message, SourceSpan span, int errorCode, Severity severity)

{

if (severity == Severity.Error || severity == Severity.FatalError)

{

m_ErrorMsg.Add(message);

}

}

}LanguageContext langContext = HostingHelpers.GetLanguageContext(engine);
SourceUnit sourceUnit = langContext.CreateSourceUnit(new SourceStringContentProvider("1/4+3"), null, SourceCodeKind.Expression); //创建代码序列:1/4+3

MyErrorSink errorSink = new MyErrorSink();
try

{

Scope scope = new Scope();

object ret = sourceUnit.Execute(scope, errorSink); //执行动态语言代码,ret就是执行结果的返回值了


if (errorSink.ErrorMsg.Count > 0) //检查编译错误

{

//

}

}
catch (Exception)

{

}