VB6 程序看不到 VB.Net COM DLL 的属性

VB6 program doesn't see properties for VB.Net COM DLL

我的客户在世界各地的大量计算机上安装了一个 VB6 程序。它现在需要通过 REST API 访问外部系统。我在 VB.Net 中创建了一个针对 .NET Framework 4 的 Class 库。(这是随处安装的最新版本。)DLL 公开了大约一打 public 函数和子例程,以及大约 40 个属性.我使用此 PowerShell 代码测试了 DLL:

[System.Reflection.Assembly]::LoadFile( "$directorypath\Debug\MyExample.dll")
$quotaInstance = new-object MyExample.ComClass1

所有方法和属性都可以从 PS 脚本访问。但是,当我将 DLL 加载到 VB6 IDE 时,对象浏览器仅显示方法,而不显示属性。如果我无法解决这个问题,我将不得不将它们重新编码为一堆函数和子例程。我讨厌那个主意。

问题 How to make properties visible to COM in a .NET DLL (Methods DO work) 看起来很相似,但我没有(明确地说,见下文)使用界面,我不确定这个问题是否有人回答过。

有谁知道发生了什么,以及(更重要的)如何解决它?谢谢!

更新:人们要求看我的代码,所以这里有一个简化的示例。我从 InterfaceId 猜测 Visual Studio Community 2017 正在为我创建某种界面。

<ComClass(Example.ClassId, Example.InterfaceId, Example.EventsId)>
Public Class Example

#Region "COM GUIDs"
    ' These  GUIDs provide the COM identity for this class 
    ' and its COM interfaces. If you change them, existing 
    ' clients will no longer be able to access the class.
    Public Const ClassId As String = "099bfc91-1f82-4a26-b1e0-d9645bb1714d"
    Public Const InterfaceId As String = "13e9763d-455c-4d89-8323-cc4e7ae7648e"
    Public Const EventsId As String = "773feeba-e635-4411-b175-30b345afa841"
#End Region

    ' A creatable COM class must have a Public Sub New() 
    ' with no parameters, otherwise, the class will not be 
    ' registered in the COM registry and cannot be created 
    ' via CreateObject.
    Public Sub New()
        MyBase.New()
    End Sub

    ' *** Auto-implemented properties
    ' When you declare an auto-implemented property, Visual Basic automatically
    ' creates a hidden Private field called the backing field to contain the
    ' Property value. The backing field name is the auto-implemented Property
    ' name preceded by an underscore (_).

    Public Property Foo As String
    Public Property Bar As Integer

    Public Function InitiateSession(ExampleServer As String) As Boolean
        ' Creates a new API_session.
        ' Returns True if the connection was successful, otherwise False
        ' ...
    End Function

    Public Sub CloseSession()
        ' Releases the current API_session.
        ' ...
    End Sub
End Class

我不是 VB 的忠实粉丝,但希望下面的示例对您有所帮助

Public Class Class1
    Implements IClass1

    Property Property_1 As Int32 Implements IClass1.Property_1
        Get
            Return Property_1
        End Get
        Set(value As Int32)
            Property_1 = value
        End Set
    End Property
End Class

Public Interface IClass1
    Property Property_1 As Int32
End Interface

如果您的属性属于通用类型,例如 List<T>,则它们无法导出到 COM。

如果是这种情况,您应该会看到来自 .NET 编译器的一些构建警告,表明存在这种情况。

(题目没有举例,不知道这里是不是这样。)

由于某些未知原因,VB 编译器为标有 ComClassAttribute 的 类 提供的自动接口生成忽略了自动实现的属性。您需要明确编码属性的长格式。

所以不是自动实现的形式:

Public Property Foo As String

你需要:

Private _Foo As String
Public Property Foo As String
    Get
        Return _Foo
    End Get
    Set(value As String)
        _Foo = value
    End Set
End Property

用于 Foo 包含在 COM 接口中。