现在的位置: 首页 > 综合 > 正文

通过比较学python(5):IronPython引用.Net类

2011年03月02日 ⁄ 综合 ⁄ 共 3176字 ⁄ 字号 评论关闭
直接Import:(限于mscorlib.dll里的所有命名空间和类)

IronPython可以方便的使用许多.net的类,而不用显式的添加引用
比如,使用from System.Collections import * 我们会可以添加对.net集合类的引用

from System.Collections import *
list1=ArrayList()
list1.Add(1)
list1.Add(2)
list1.Add(3)
print list.Count
ht=Hashtable()
ht.Add(1,1)
ht.Add('2',2)

print ('aaaa')
先添加引用,再import
凡是mscorlib.dll里的所有命名空间和类都是可以用上面的方法直接import,因为这个mscorlib.dll是默认引用的
(通过ipy.exe -X:SaveAssemblies:可以把ipy生成的程序集缓存下来,用反编译工具查看就可以看到)但是如果需要引入其他模块中的类,
就需要通过clr模块提供的添加引用的一系列方法,就好像在写.net程序的时候需要先添加引用,再using一样,
比如如果你希望使用System.Xml命名空间下的类
import clr
clr.AddReference("System.Xml")
from System.Xml import *
doc=XmlDocument()
doc.Load("test.xml")

用反编译工具查看生成的程序集,可以发现程序集并没有增加引用,在程序中我们可以看到,对引用的添加是通过反射动态进行的

关于引用的有趣的现象

另外还有有趣的现象,当引用了.net类后会添加,python对象会多出许多方法和属性,也就是说python对象会具有很多的.net属性和方法,下面我们就来看看这个现象,首先我创建了一个list,然后通过dir方法展示初list的所有方法,然后,我们import clr,或者import System,在把刚才创建的list对象的所有属性和方法展示出来

a=[1,2,3]
method=dir(a)
print 'before import'
print method
import clr
method=dir(a)
print 'after import'
print method 

输出为

before import
['__add__', '__class__', '__contains__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__getitem__', '__getslice__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__len__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setitem__', '__setslice__', '__str__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
after import
['Add', 'AddSequence', 'Append', 'Clear', 'CompareTo', 'Contains', 'ContainsValue', 'CopyTo', 'Count', 'DeleteItem', 'DeleteSlice', 'Equals', 'Extend', 'Finalize', 'GetDynamicType', 'GetEnumerator', 'GetHashCode', 'GetLength', 'GetObjectArray', 'GetSlice', 'GetType', 'InPlaceAdd', 'InPlaceMultiply', 'Index', 'IndexOf', 'Initialize', 'Insert', 'IsFixedSize', 'IsReadOnly', 'IsSynchronized', 'ListEnumeratorCollection', 'Make', 'MakeDynamicType', 'MakeEmptyList', 'MakeList', 'MemberwiseClone', 'MultiplySequence', 'Pop', 'Reduce', 'ReferenceEquals', 'Remove', 'RemoveAt', 'Reverse', 'ReverseMultiply', 'RichEquals', 'RichGetHashCode', 'RichNotEquals', 'SetSlice', 'Sort', 'SyncRoot', 'ToCodeString', 'ToString', '__add__', '__class__', '__contains__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__getitem__', '__getslice__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__len__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setitem__', '__setslice__', '__str__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

通过比较,我们可以发现在import了.net的命名空间或者类后,列表增加了许多大写字母开头的方法和属性,这样做的作用也许是方便python对象和.net对象的交互,比如用下面的语句初始化一个ArrayList是成立的

list=ArrayList([1,2,3])
len(list)
我们可以看到list的长度为三,也就是说我们通过把一个python的list赋值给ArrayList的构造函数,从而创建了一个ArrayList,我们知道
ArrayList的构造函数可以接受一个ICollection作为参数,从而初始化ArrayList,这说明python内置的list类一定从ICollection继承,
那么他肯定具有ICollection所提供的方法,然后在没有引入这个.net命名空间或者类的时候,我们用dir命令没有看到这些方法,
因为这个时候这些方法不是必要的,内置列表的方法已经足够用了,然而一旦发生引用,那么像刚才那种互操作就有可能发生,
所以这些方法都必须展示出来,准备被调用
import与using的区别
1)import可以引入一个类,比如import System.Collections.ArrayList,而using只能引用命名空间
2)import引入一个类后,我们仍然要使用带命名空间的完整命名去使用他,比如在通过import System.Collections.ArrayList后希望使用
ArrayList,必须这样用
arrayList=System.Collections.ArrayList()
直接写arrayList=ArrayList()会出错,而using一旦引用一个命名空间,就可以不用加命名空间的前缀,直接使用这个类.如果想要在python中
方便的使用这些类,请使用from ... import ...
 

抱歉!评论已关闭.