我的客户有一个 VB6 程序安装在世界各地的大量计算机上。现在它需要通过 REST API 访问外部系统。我在 VB.Net 中创建了一个针对 .NET Framework 4 的类库。(这是随处安装的最新版本。)该 DLL 公开了大约十几个公共函数和子例程,以及大约 40 个属性。我使用以下 PowerShell 代码测试了 DLL:
[System.Reflection.Assembly]::LoadFile( "$directorypath\Debug\MyExample.dll")
$quotaInstance = new-object MyExample.ComClass1
Run Code Online (Sandbox Code Playgroud)
所有方法和属性都可以从 PS 脚本访问。然而,当我将 DLL 加载到 VB6 IDE 中时,对象浏览器仅显示方法,而不显示属性。如果我无法弄清楚这一点,我将不得不将它们重新编码为一堆函数和子例程。我讨厌这个想法。
问题How to makepropertiesvisible 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
Run Code Online (Sandbox Code Playgroud)
由于某些未知的原因,VB 编译器为标有ComClassAttribute 的类提供的自动接口生成会忽略自动实现的属性。您需要显式地对属性的长格式进行编码。
因此,而不是自动实现的形式:
Public Property Foo As String
Run Code Online (Sandbox Code Playgroud)
你需要:
Private _Foo As String
Public Property Foo As String
Get
Return _Foo
End Get
Set(value As String)
_Foo = value
End Set
End Property
Run Code Online (Sandbox Code Playgroud)
以便Foo
包含在 COM 接口中。
归档时间: |
|
查看次数: |
1100 次 |
最近记录: |