【发布时间】:2020-05-10 13:18:08
【问题描述】:
我在 Excel VBA 中使用 .NET 库类。 有用。我可以通过 RegAsm 编译和注册它。 使用 .Net 中的 Intercaces 和 COM 属性,例如 ([InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]) ,通常我会看到方法和对象 在 VBA Intelisence 中,我可以使用它
但是...我想使用一个复杂的多级对象域。通过某种方法获取它,我在将子对象列表作为父对象的属性时遇到问题。
我在 C# 中的方法
public object GetParentWithChildList()
{
var parent = new Parent
{
ParentName = "John",
Children = new List<object>
{
new Child {ChildName = "Tom"},
new Child {ChildName = "Brian"},
new Child {ChildName = "Eva"}
}.ToArray()
};
return parent;
}
public class Parent
{
public string ParentName { get; set; }
public object[] Children { get; set; }
}
public class Child
{
public string ChildName { get; set; }
}
object[] ,我认为是返回 VBA/VB6 对象列表的最佳方式 当我有 1 个级别的对象时它可以工作。例如我想只返回对象列表。 object[] 比 Child[] 或 List 更好用
而在 VBA 中它只能部分工作:
(主对象调用 LibDataAccess)
Sub GetParentWithChildListVbaTest()
Dim qda As LibDataAccess
Dim parent As parent
Set qda = New QgeDataAccess
Set parent = qda.GetParentWithChildList()
Debug.Print (parent.ParentName) ' Works OK - it returns John
Dim child As child
Set child = parent.Children(0) ' This line returns error: Wrong number of arguments or invalid property assignment
Debug.Print (parent.Children(0).ChildName)
End Sub
我的问题是: 如何返回具有子对象列表作为属性的对象(在 .NET 中为 VBA 准备)? 我在 C# 或 VBA 中有错误的代码吗?
【问题讨论】:
-
COM 没有泛型的概念。您必须创建某种包装器。见:stackoverflow.com/questions/1474100/…
-
是的,你说得对。这就是为什么我使用 object[] Children 而不是 IList
Children。但问题在于在 vba 下阅读它