获取 UNO 对象信息

From Apache OpenOffice Wiki
Jump to: navigation, search




Basic RTL 可以获取 UNO 对象的信息。提供了在运行时分析对象的函数以及用于在调试时检查对象的对象属性。


运行时检查接口

尽管 Basic 不像 C++ 和 Java 那样支持 queryInterface 概念,但了解一个 UNO Basic 对象是否支持某个具体接口非常有用。函数 HasUnoInterfaces() 用于进行此项检测。

第一个参数 HasUnoInterfaces() 需要的是应该接受测试的对象。接着可以将一个或多个全限定的接口名称参数传送到该函数。如果对象支持所有这些接口,则函数返回 True,否则返回 False

 Sub Main
   Dim oSimpleFileAccess
   oSimpleFileAccess = CreateUnoService( "com.sun.star.ucb.SimpleFileAccess" )
 
 Dim bSuccess
     Dim IfaceName1$, IfaceName2$, IfaceName3$
     IfaceName1$ = "com.sun.star.uno.XInterface"
     IfaceName2$ = "com.sun.star.ucb.XSimpleFileAccess2"
     IfaceName3$ = "com.sun.star.container.XPropertySet"
     
     bSuccess = HasUnoInterfaces( oSimpleFileAccess, IfaceName1$ )
     MsgBox bSuccess ' Displays True because XInterface is supported
     
     bSuccess = HasUnoInterfaces( oSimpleFileAccess, IfaceName1$, IfaceName2$ )
     MsgBox bSuccess ' Displays True because XInterface
     ' and XSimpleFileAccess2 are supported
 
     bSuccess = HasUnoInterfaces( oSimpleFileAccess, IfaceName3$ )
     MsgBox bSuccess ' Displays False because XPropertySet is NOT supported
   
     bSuccess = HasUnoInterfaces( oSimpleFileAccess, IfaceName1$, IfaceName2$, IfaceName3$ )
     MsgBox bSuccess ' Displays False because XPropertySet is NOT supported
 End Sub


运行时测试对象是否为一个结构

如前面 专业 UNO - UNO 语言绑定 - OpenOffice.org Basic - 类型映射 - 结构的映射 一节中所述,结构的处理方式不同于对象,因为处理时结构将被视作值。使用 IsUnoStruct () 函数检查 UNO Basic 对象是一个对象还是一个结构。该函数需要一个参数,并且,如果该参数是一个 UNO 结构,函数返回 True,否则返回 False。示例:

 Sub Main
     Dim bIsStruct
         ' Instantiate a service
     Dim oSimpleFileAccess
     oSimpleFileAccess = CreateUnoService( "com.sun.star.ucb.SimpleFileAccess" )
     bIsStruct = IsUnoStruct( oSimpleFileAccess )
     MsgBox bIsStruct ' Displays False because oSimpleFileAccess is NO struct
                      ' Instantiate a Property struct
     Dim aProperty As New com.sun.star.beans.Property
     bIsStruct = IsUnoStruct( aProperty )
     MsgBox bIsStruct ' Displays True because aProperty is a struct
     bIsStruct = IsUnoStruct( 42 )
     MsgBox bIsStruct ' Displays False because 42 is NO struct
 End Sub


运行时测试对象的身份

要查明两个 UNO OpenOffice.org Basic 对象是否引用同一个 UNO 对象实例,可以使用函数 EqualUnoObjects()。在 Basic 中,不能对对象类型的参数应用比较运算符 =,例如 If Obj1 = Obj2 Then 会导致运行时错误。

 Sub Main
     Dim bIdentical
     Dim oSimpleFileAccess, oSimpleFileAccess2, oSimpleFileAccess3
         ' Instantiate a service
     oSimpleFileAccess = CreateUnoService( "com.sun.star.ucb.SimpleFileAccess" )
     oSimpleFileAccess2 = oSimpleFileAccess ' Copy the object reference
     bIdentical = EqualUnoObjects( oSimpleFileAccess, oSimpleFileAccess2 )
     MsgBox bIdentical ' Displays True because the objects are identical
         ' Instantiate the service a second time
     oSimpleFileAccess3 = CreateUnoService( "com.sun.star.ucb.SimpleFileAccess" )
     bIdentical = EqualUnoObjects( oSimpleFileAccess, oSimpleFileAccess3 )
     MsgBox bIdentical ' Displays False, oSimpleFileAccess3 is another instance
 
     bIdentical = EqualUnoObjects( oSimpleFileAccess, 42 )
     MsgBox bIdentical ' Displays False, 42 is not even an object
         ' Instantiate a Property struct
     Dim aProperty As New com.sun.star.beans.Property
     Dim aProperty2
     aProperty2 = aProperty ' Copy the struct
     bIdentical = EqualUnoObjects( aProperty, aProperty2 )
     MsgBox bIdentical ' Displays False because structs are values
         ' and so aProperty2 is a copy of aProperty
 End Sub


Basic 将接口隐藏在 OpenOffice.org Basic 对象后面,开发者使用 API 结构时,这些接口可能会导致发生问题。了解 API 引用并找到一个正确的方法访问对象并达到某种具体目标,这比较难。


调试时检查接口

Dbg_SupportedInterfaces 列出了对象支持的所有接口。在以下示例中,将前一小节中说明的函数 GetProcessServiceManager() 所返回的对象作为示例对象。

 oServiceManager = GetProcessServiceManager()
 MsgBox oServiceManager.Dbg_SupportedInterfaces

该调用显示一个消息框:

Dbg_SupportedInterfaces 属性

此列表包含对象支持的所有接口。对于从其他接口派生的接口,父接口被缩进,如上面所示的 com.sun.star.container.XSet,它是从 com.sun.star.container.XEnumerationAccess 派生的,而后者又是从 com.sun.star.container.XElementAccess 派生的。

Documentation note.png 如果接口名称后面打印有文字 “(ERROR: Not really supported!)”,则该对象实现通常会包含错误,因为对象

声称支持此接口(每个 com.sun.star.lang.XTypeProvider,但查询该接口时却失败。如果需要详细信息,请参阅 高级 UNO - 语言绑定 - UNO 反射 API)。


调试时检查属性

Dbg_Properties 列出对象支持的所有属性,借助的是 com.sun.star.beans.XPropertySet 以及可以映射成 Basic 对象属性的 get 和 set 方法:

 oServiceManager = GetProcessServiceManager()
 MsgBox oServiceManager.Dbg_Properties

该代码产生如以下示例所示的消息框:

Dbg_Properties


调试时检查方法

Dbg_Methods 列出对象支持的所有方法。示例:

 oServiceManager = GetProcessServiceManager()
 MsgBox oServiceManager.Dbg_Methods

该代码显示:

Dbg_Methods


Dbg_PropertiesDbg_Methods 中使用的表示法会引用到 Basic 中的内部实现类型名称。可以忽略 Sbx 前缀。其他名称与一般 Basic 类型表示法对应。SbxEMPTYVariant 的类型相同。下一节介绍 Basic 类型的更多信息。

Documentation note.png Basic 使用 com.sun.star.lang.XTypeProvider 接口来检查某对象支持哪些接口。因此,实现应该可以从 Basic 访问的组件时,支持此接口十分重要。如果需要详细信息,请参阅 编写 UNO 组件
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages