[集成IronPython] 添加CLR对象到运行环境
2008-07-28 09:17 Colin Han 阅读(7163) 评论(12) 编辑 收藏 举报动态类型语言(以下简称:"动态语言"),在10年前就已流行起来。JavaScript更是成为了WEB前台开发的事实标准。但它们进入普通开发 人员的视野也就在近几年。随着Web2.0和敏捷开发方法论的兴起,动态语言的灵活高效的特性成为了它被更多项目选择和使用的理由。一些大型网站已开始使 用动态语言来实现,其中,国内比较优秀的作品有“豆瓣”。微软更是不甘落后,建立了DLR(动态语言运行时)来吸引动态语言爱好者在其上实现动态语言。IronPython就是其主要成员之一。
在本系列文章中,我们将逐步实现一个自定义控件,实现类似IDE的Immediate窗口的功能。用户可以在其中输入和运行IronPython代码。 【返回目录】
在上一节中,我们建立了IronPython运行环境,并且在其中成功的运行了“print "hello world"”的命令。正如上一节的讨论中所看到的,大家都很关心如何让IronPython脚本和我们的运行环境交互。在这一节中,我们将会在DLConsole上添加两个方法,实现将运行环境中的Clr对象传递给IronPython脚本使用。
大家可以从这里下载可运行的源代码。
1. 先看运行效果。
我在DLConsole中输入如下脚本:

2

3

4

5

6

7

8

9

10

运行结果,如图:
2. 确保脚本可以访问CLR对象
首先,我们需要脚本能够成功的访问CLR对象。我在DLConsole上暴露了两个方法。分别是:

2

3

4

5

6

7

8

9

10

11

在上一讲的源代码中,我们保存了一个ScriptScope对象。用户输入的脚本就是在这个Scope对象上运行的。因此,我们在这里向ScriptScope中添加的变量,就能够被用户输入的脚本所访问了。
在测试程序中,我添加了如下的代码来将对象注册到脚本运行环境中。

2

3

4

5

6

7

8

9

10

这时,我们就可以实现类似“Button1.Text = "Hello IronPython"”和“Button1.BackColor = TextBox1.ForeColor”这样的脚本了。但是,你会发现,如果你输入类似“Button1.BackColor = Color.Red”的脚本,会产生一个“name 'Color' is not defined”的异常。是因为在目前的Scope中无法找到Color的定义引起的。
3. 使用CLR类型
如果需要在IronPython中使用CLR类型,需要完成以下几个步骤:
a) 引入clr对象:这个对象实现了对CLR访问的一些必要的操作。例如:添加引用。
b) 添加引用:通过clr.AddReference方法,你可以使用Assembly的名字作为参数来添加对这个Assembly的引用。IronPython会使用.NET加载Assembly的策略去寻找和加载这个Assembly。
c) 引入变量:通过IronPython的“import”或“from ... import ...”语法,将你需要使用的类型引入当前Scope。
之后,你就可以使用类似“x = Form()”语法构建一个对象或通过类似“Color.Red”的语法访问对象的静态成员。
最后,给一个比较复杂的例子,从中可以理解一下IronPython的语法。

2

3

4

5

6

7

8

9

10

11

12

13

运行效果:
需要说明一下,因为在控制台上输入的脚本都是在同一个Scope对象上运行的,因此,import语句只需要运行一次就可以了。也就是说,上面的代码执行后,你可以继续使用Form、Button等类型。
4. 总结
微软在DLR和CLR之间交互方面的考虑很多,因此,对一些简单的操作,我们甚至感觉不到CLR对象和一个IronPython对象之间的区别。当然,动态语言的优势即在于它的动态类型系统,这一点和CLR的静态类型系统有很大的差别。因此很多IronPython的特性(例如:动态添加属性到对象上)无法在CLR对象上使用。在后面的章节中,我们将通过一些扩展接口实现更高级的嵌入。
另一方面,将方法注册到动态语言运行环境中也是大家比较关心的一个话题。虽然,通过注册对象到运行环境,已经可以在脚本中访问这些对象的方法。但毕竟不如直接使用方法来的简单。下一节将会详细的讨论相关的实现。敬请关注
免责声明:本系列文章,完全是我个人研读IronPython源代码后找到的实现方案,并未详细的研究过IronPython的相关官方文档。因此,并不保证符合微软DLR和IronPython的设计思路,亦不能保证 在DLR和IronPython 2.0正式发布后能够继续使用。