开发者

VB10, Auto-Implemented Properties and COM

开发者 https://www.devze.com 2023-01-27 12:30 出处:网络
I recently finished a class that we\'re using to tie Access to some WCF Services.Of course this means that the .Net classes (and all of their properties) need to be visible to COM.Given that I\'m usin

I recently finished a class that we're using to tie Access to some WCF Services. Of course this means that the .Net classes (and all of their properties) need to be visible to COM. Given that I'm using VB10 and the Contact class has about 20 properties I went ahead and used auto-implementing properties.

Much to my surprise, the properties were not accessible from within VBA in Access. I tried marking the properties as ComVisible (which I didn't have to do in the past with standard properties) and it still didn't work. After changing the auto properties to st开发者_运维知识库andard properties everything worked.

Public Property FirstName As String

Became

Public Property FirstName As String
    Get
        return _strFirstName
    End Get
    Set
        _strFirstName = value
    End Set
End Property

My understanding is that the two should be equivalent. According to what I've read on MSDN, auto-implementing properties simply take care of creating the backing field and getter / setter for you and for all intents and purposes they should be the same.

Clearly they're not, so what else is going on behind the scenes?


They are. Some sample code:

<ComVisible(True)> _
<ClassInterface(ClassInterfaceType.AutoDual)> _
Public Class Class1
    Private prop As Boolean
    Public Property BoolProp() As Boolean
        Get
            Return prop
        End Get
        Set(ByVal value As Boolean)
            prop = value
        End Set
    End Property

    Public Property BoolProp2() As Boolean
End Class

With commands:

tlbexp ClassLibrary1.dll
oleview ClassLibrary2.tlb

Produces this interface dump:

interface _Class1 : IDispatch {
    [id(00000000), propget,
      custom(54FC8F55-38DE-4703-9C4E-250351302B1C, 1)]
    HRESULT ToString([out, retval] BSTR* pRetVal);
    [id(0x60020001)]
    HRESULT Equals(
                    [in] VARIANT obj, 
                    [out, retval] VARIANT_BOOL* pRetVal);
    [id(0x60020002)]
    HRESULT GetHashCode([out, retval] long* pRetVal);
    [id(0x60020003)]
    HRESULT GetType([out, retval] _Type** pRetVal);
    [id(0x60020004), propget]
    HRESULT BoolProp([out, retval] VARIANT_BOOL* pRetVal);
    [id(0x60020004), propput]
    HRESULT BoolProp([in] VARIANT_BOOL pRetVal);
    [id(0x60020006), propget]
    HRESULT BoolProp2([out, retval] VARIANT_BOOL* pRetVal);
    [id(0x60020006), propput]
    HRESULT BoolProp2([in] VARIANT_BOOL pRetVal);
};

It's there. You are doing something wrong, no idea what.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号