Using VB.net Collection class via COM returns error: "Class doesn't support automation"

Using VB.net Collection class via COM returns error: "Class doesn't support automation"

我有一个现有的 VB.net class 库,其中有一个 public 属性 类型为 VB 的 Collection class。我将 class 库作为 COM 对象公开,以便能够在 Progress 中使用它。

当我使用整数索引(例如 comObj.OutputCol.Item(1))访问 Collection-属性 时,它工作正常,但是当我尝试使用字符串索引器(例如 comObj.OutputCol.Item("FirstCol"))时,我收到以下错误(来自我用于测试的 VB 脚本):

Error message: Class doesn't support automation
Error code: 800A01AE

是否可以通过 COM 以任何方式使用字符串索引器?

示例代码,COM 对象 i VB.net:

<ComClass(TestClass.ClassId, TestClass.InterfaceId, TestClass.EventsId)>
Public Class TestClass
    Public Const ClassId As String = "063CA388-9926-44EC-B3A6-856D5299C210"
    Public Const InterfaceId As String = "094ECC57-4E84-423A-B20E-BD109AEDBC20"
    Public Const EventsId As String = "038B18BD-54B4-42D3-B868-71F4C52345B0"

    Private _sOutputCol As Collection = Nothing
    Private Property sOutputCol() As Collection
        Get
            If _sOutputCol Is Nothing Then
                _sOutputCol = New Collection()
            End If
            Return _sOutputCol
        End Get
        Set(ByVal Value As Collection)
            _sOutputCol = Value
        End Set
    End Property

    Public ReadOnly Property OutputCol() As Collection
        Get
            Return sOutputCol
        End Get
    End Property

    Public Sub New()
        sOutputCol.Add("First object", "FirstCol")
        sOutputCol.Add(2, "SecondCol")
    End Sub

End Class

VB脚本中的示例测试代码:

Set comObj = WScript.CreateObject("VbComTest.TestClass")
wscript.echo comObj.OutputCol.Item(1) ' Works
wscript.echo comObj.OutputCol.Item(CStr("FirstCol")) ' Gives the error

我已将 dll 注册到:>regasm "...path...\VbComTest.dll" /codebase

好的,问题是索引器过载,您不应该在 COM 可见接口中使用它:https://msdn.microsoft.com/en-us/library/ms182197.aspx

从有关重载方法发生的情况的页面中摘录:

When overloaded methods are exposed to COM clients, only the first method overload retains its name. Subsequent overloads are uniquely renamed by appending to the name an underscore character '_' and an integer that corresponds to the order of declaration of the overload. For example, consider the following methods.

void SomeMethod(int valueOne); void SomeMethod(int valueOne, int valueTwo, int valueThree); void SomeMethod(int valueOne, int valueTwo);

These methods are exposed to COM clients as the following.

void SomeMethod(int valueOne); void SomeMethod_2(int valueOne, int valueTwo, int valueThree); void SomeMethod_3(int valueOne, int valueTwo);

Visual Basic 6 COM clients cannot implement interface methods by using an underscore in the name.

所以要使用字符串索引器,我必须这样写:

wscript.echo comObj.OutputCol.Item_3("FirstCol")

(Item_2 将对象作为参数,如果文档正确,也可以使用。