You can use the as operator
to perform checked interface typecasts. This is known as interface querying,
and it yields an interface-type expression from an object reference or from
another interface reference, based on the actual (runtime) type of the object.
An interface query has the form
object as interface
where object is an expression of an
interface or variant type or denotes an instance of a class that implements an
interface, and interface is any interface declared with a GUID.
An interface query returns nil if object is nil. Otherwise, it passes the GUID of interface to the QueryInterface method in object, raising an exception unless QueryInterface returns zero. If QueryInterface returns zero (indicating that object’s class implements interface), the interface query returns an interface reference to object.
所谓接口查询(interface
querying),就是使用as运算符来执行对接口类型转换的检查。接口查询基于对象的实际(运行时)类型,从对象引用或另一个接口引用产生一个接口类型的表达式。接口查询具有如下形式,
object as interface
这里的的object是一个表达式,该表达式的类型是接口类型、变体类型或表示实现接口的类;interface是任何一个用GUID声明的接口。
如果object为nil,那么接口查询返回nil。否则,接口查询将传递interface的GUID到object中的QueryInterface方法,除非QueryInterface返回零,否则此举将引发异常。如果QueryInterface返回零(表示对象object的类实现了接口interface),那么接口查询将返回对对象object的接口引用。