Difference between revisions of "Zh/Documentation/DevGuide/ProUNO/Basic/Getting Information about UNO Objects"
m |
|||
Line 117: | Line 117: | ||
此列表包含对象支持的所有接口。对于从其他接口派生的接口,父接口被缩进,如上面所示的 <idl>com.sun.star.container.XSet</idl>,它是从 <idl>com.sun.star.container.XEnumerationAccess</idl> 派生的,而后者又是从 <idl>com.sun.star.container.XElementAccess</idl> 派生的。 | 此列表包含对象支持的所有接口。对于从其他接口派生的接口,父接口被缩进,如上面所示的 <idl>com.sun.star.container.XSet</idl>,它是从 <idl>com.sun.star.container.XEnumerationAccess</idl> 派生的,而后者又是从 <idl>com.sun.star.container.XElementAccess</idl> 派生的。 | ||
− | {{ | + | {{Note|如果接口名称后面打印有文字 “(ERROR: Not really supported!)”,则该对象实现通常会包含错误,因为对象 |
声称支持此接口(每个 <idl>com.sun.star.lang.XTypeProvider</idl>,但查询该接口时却失败。如果需要详细信息,请参阅 [[Documentation/DevGuide/AdvUNO/UNO Reflection API|高级 UNO - 语言绑定 - UNO 反射 API]])。}} | 声称支持此接口(每个 <idl>com.sun.star.lang.XTypeProvider</idl>,但查询该接口时却失败。如果需要详细信息,请参阅 [[Documentation/DevGuide/AdvUNO/UNO Reflection API|高级 UNO - 语言绑定 - UNO 反射 API]])。}} | ||
Line 151: | Line 151: | ||
<code>Dbg_Properties</code> 和 <code>Dbg_Methods</code> 中使用的表示法会引用到 Basic 中的内部实现类型名称。可以忽略 <code>Sbx</code> 前缀。其他名称与一般 Basic 类型表示法对应。<code>SbxEMPTY</code> 与 <code>Variant</code> 的类型相同。下一节介绍 Basic 类型的更多信息。 | <code>Dbg_Properties</code> 和 <code>Dbg_Methods</code> 中使用的表示法会引用到 Basic 中的内部实现类型名称。可以忽略 <code>Sbx</code> 前缀。其他名称与一般 Basic 类型表示法对应。<code>SbxEMPTY</code> 与 <code>Variant</code> 的类型相同。下一节介绍 Basic 类型的更多信息。 | ||
− | {{ | + | {{Note|Basic 使用 <idl>com.sun.star.lang.XTypeProvider</idl> 接口来检查某对象支持哪些接口。因此,实现应该可以从 Basic 访问的组件时,支持此接口十分重要。如果需要详细信息,请参阅 [[Zh/Documentation/DevGuide/WritingUNO/Writing UNO Components|编写 UNO 组件]]。}} |
{{PDL1}} | {{PDL1}} | ||
[[Category:文档/开发者指南/专业 UNO]] | [[Category:文档/开发者指南/专业 UNO]] |
Latest revision as of 17:06, 4 July 2018
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
该调用显示一个消息框:
此列表包含对象支持的所有接口。对于从其他接口派生的接口,父接口被缩进,如上面所示的 com.sun.star.container.XSet,它是从 com.sun.star.container.XEnumerationAccess 派生的,而后者又是从 com.sun.star.container.XElementAccess 派生的。
如果接口名称后面打印有文字 “(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_Methods
列出对象支持的所有方法。示例:
oServiceManager = GetProcessServiceManager() MsgBox oServiceManager.Dbg_Methods
该代码显示:
Dbg_Properties
和 Dbg_Methods
中使用的表示法会引用到 Basic 中的内部实现类型名称。可以忽略 Sbx
前缀。其他名称与一般 Basic 类型表示法对应。SbxEMPTY
与 Variant
的类型相同。下一节介绍 Basic 类型的更多信息。
Basic 使用 com.sun.star.lang.XTypeProvider 接口来检查某对象支持哪些接口。因此,实现应该可以从 Basic 访问的组件时,支持此接口十分重要。如果需要详细信息,请参阅 编写 UNO 组件。 |
Content on this page is licensed under the Public Documentation License (PDL). |