4

我有一个当前正在编译的 VB6 项目,即使我正在访问一个不存在的属性。

代码看起来有点像这样:

Public vizSvrEmp As VizualServer.Employees
Set vizSvrEmp = New VizualServer.Employees

Fn = FreeFile
Open vizInfo.Root & "FILE.DAT" For Random As #Fn Len = RecLen
Do While Not EOF(Fn)
    Get #Fn, , ClkRecord
    With vizSvrEmp
        Index = .Add(ClkRecord.No)
        .NotAvailable(Index) = ClkRecord.NotAvailable
        .Bananas(Index) = ClkRecord.Start
        'Plus lots more properties
    End With
Loop

Bananas属性在对象中不存在,但仍可编译。我的vizSvrEmp对象是一个 .NET COM 互操作 DLL 并且是早期绑定的,如果我在其中键入点,我会正确获得 Intellisense(它不显示Bananas)

我尝试删除With但行为相同

我如何确保编译器能够识别这些错误?

4

1 回答 1

3

我知道你已经在 Hans 的帮助下解决了这个问题,但为了完整起见,使用的替代方法ClassInterface(ClassInterfaceType.AutoDual)是使用ClassInterface(ClassInterfaceType.None)然后实现一个用InterfaceType(ComInterfaceType.InterfaceIsDual)>.

它需要更多的工作,但可以让您完全控制界面 GUID。AutoDual 将在您编译时自动为接口生成唯一的 GUID,这样可以节省时间,但您无法控制它们。

在使用中,这看起来像这样:

<ComVisible(True), _
Guid(Guids.IEmployeeGuid), _
InterfaceType(ComInterfaceType.InterfaceIsDual)> _
Public Interface IEmployee 

   <DispIdAttribute(1)> _
   ReadOnly Property FirstName() As String

   <DispIdAttribute(2)> _
   ReadOnly Property LastName() As String

   <DispIdAttribute(3)> _
   Function EtcEtc(ByVal arg As String) As Boolean

End Interface


<ComVisible(True), _
Guid(Guids.EmployeeGuid), _
ClassInterface(ClassInterfaceType.None)> _
Public NotInheritable Class Employee
   Implements IEmployee 

   Public ReadOnly Property FirstName() As String Implements IEmployee.FirstName
      Get
         Return "Santa"
      End Get
   End Function

   'etc, etc

End Class

请注意 GUID 是如何声明的。我发现创建一个帮助类来整合 GUID 并提供 Intellisense 效果很好:

Friend Class Guids
   Public Const AssemblyGuid As String = "BEFFC920-75D2-4e59-BE49-531EEAE35534"   
   Public Const IEmployeeGuid As String = "EF0FF26B-29EB-4d0a-A7E1-687370C58F3C"
   Public Const EmployeeGuid As String = "DE01FFF0-F9CB-42a9-8EC3-4967B451DE40"
End Class

最后,我在装配级别使用这些:

'The following GUID is for the ID of the typelib if this project is exposed to COM
<Assembly: Guid(Guids.AssemblyGuid)> 

'NOTE:  The following attribute explicitly hides the classes, methods, etc in 
'        this assembly from being exported to a TypeLib.  We can then explicitly 
'        expose just the ones we need to on a case-by-case basis.
<Assembly: ComVisible(False)> 
<Assembly: ClassInterface(ClassInterfaceType.None)> 
于 2012-11-30T14:43:21.073 回答