编译器不会报错了当COM属性是不正确/缺失?

问题描述 投票:4回答:1

我目前编译即使我访问一个不存在的属性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,并早期绑定,如果我在键入点我正确地获得智能感知(不显示香蕉)

我试图消除With但相同的行为

我怎样才能确保这些错误是由编译器拾起?

vb.net compiler-errors vb6 com-interop
1个回答
3
投票

我知道你有这个整理出来与汉斯的帮助,但只是为了完整起见,替代使用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和提供智能感知工作进行的顺利:

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)> 
© www.soinside.com 2019 - 2024. All rights reserved.