如何使用 VB.net 读取作为 COM__Object 传递的自定义 VBA class 对象的成员? (用户界面路径)

How to read members of a custom VBA class object, passed as a COM__Object, with VB.net? (UIPath)

我有一个 VBA 函数,它 returns 我自己的一个对象 class 模块 "CInputDBEntries"。

UIPath 将此对象作为 COM__Object 接收,要读取它,我需要进行一些 CType 转换。但是无论我怎么做,我都无法访问对象的成员(值)。

我已经尝试将其转换为 "Object" 但无法访问这些值。 我还尝试将多个 class 对象保存为一个集合,但它在 CType 转换时失败,因为它有问题(VBACollection to Microsoft.VisualBasic.Collection = COM CastTypeException)

我也在 UIPath 论坛上发布了这个:https://forum.uipath.com/t/invoke-vba-how-to-read-the-retrieved-com-object-in-uipath-vba-function-returns-custom-type/96115

Class定义:

Private strTitle As String
Private strValue As String
Private boolIntegrity As Boolean

方法:

Public Function ReadRelevantEntries() As CInputDBEntries
   Dim entry As CInputDBEntries

   entry.Title = "title"
   entry.Val = "value"
   entry.Integrity = True

   Set ReadRelevantEntries = entry
End Function

目前我的转换是这样的:CType(listExcelEntries, Object)

预期结果是能够从 COM__Object 中提取值,这样我就可以读取 2 个字符串和我的 bool 变量。

找到解决方案:

这是错误的做法

var propertyInfo = comObject.GetType().GetProperty("PropertyName")

这是正确的做法

//get the value of comObject.PropertyName
object propertyValue = comObject.GetType().InvokeMember("PropertyName", System.Reflection.BindingFlags.GetProperty, null, comObject, null);

致谢:https://smehrozalam.wordpress.com/2010/02/16/c-using-reflection-with-com-objects/