通过COM Interop将集合公开给VB6应用程序

时间:2015-01-22 20:05:55

标签: vb.net com vb6 com-interop

我正在尝试将以下函数添加到我的COM类库中,它将无法编译。我相信你不能将收藏品暴露给COM。我想通过使用RegAsm并在我的VS2013机器和VB6机器上注册Microsoft.VisualBasic.dll作为tlb它可以工作,但它没有。

如果没有,那么将某种对象列表传递给VB6应用程序的最佳方法是什么。

    Public Function GetCustomerCollection() As Collection
    Dim collection As New Collection

    Dim c1 As New customer
    c1.Name = "Test Customer1"
    c1.Phone = "(888) 777-9443"
    c1.Balance = 22.58

    Dim c2 As New customer
    c2 .Name = "Test Customer2"
    c2 .Phone = "(888) 433-4423"
    c2 .Balance = 99.99

    collection.Add(c1)
    collection.Add(c2)

    Return collection
End Function

更新
在VB6中,我正在尝试检索列表:

Private Sub Form_Load()
Dim demo As New testLibrary.Demo

Dim customerList As Collection
Set customerList = demo.GetCustomerCollection

Label1.Caption = customerList(1)
End Sub

1 个答案:

答案 0 :(得分:3)

不,Microsoft.VisualBasic.Collection类不是<ComVisible(True)>。 System.Collections中的任何.NET 1.x接口和集合类都很好。

COM方式只是暴露接口,因此如果VB6代码不应该修改集合,则使用IEnumerable,如果是,则使用IList。所以你想这样写:

Public Function GetCustomerCollection() As System.Collections.IEnumerable
    Dim collection As New List(Of customer)

    collection.Add(New customer With {.Name = "Test Customer1", .Phone = "(888) 777-9443", .Balance = 22.58})
    collection.Add(New customer With {.Name = "Test Customer2", .Phone = "(888) 433-4423", .Balance = 99.99})

    Return collection
End Function

如果需要修改,请替换IList。