Exposing inherited members of a COM vb.net class

2019-08-20 08:04发布

问题:

I have two vb.net class:

Public MustInherit Class Class1
    Private m_sProperty1 As String = ""
    Public Property sProperty1() As String
        Get
            Return m_sProperty1
        End Get
        Set(ByVal value As String)
            m_sProperty1 = value
        End Set
    End Property
End Class

<ComClass("classid","interfaceid","eventid")> _
Public Class Class2
    Inherits Class1

    Private m_sProperty2 As String = ""
    Public Property sProperty2() As String
        Get
            Return m_sProperty2
        End Get
        Set(ByVal value As String)
            m_sProperty2 = value
        End Set
    End Property
End Class

When I reference the compiled DLL through VB6, Class2 only exposes sProperty2. How can I access the inherited property of the base class (sProperty1) through COM?

回答1:

This seems to answer your question:

Exporting Inheritance Hierarchies

Managed class hierarchies flatten out when exposed as COM objects. For example, if you define a base class with a member, and then inherit the base class in a derived class that is exposed as a COM object, clients that use the derived class in the COM object will not be able to use the inherited members. Base class members are accessible from COM objects only as instances of a base class, and then only if the base class is also created as a COM object.

Taken from here: Troubleshooting Interoperability



回答2:

I'll take a guess as I'm not up to speed on .NET

I would imagine your declaration is taking your public methods of Class2 and creating a COM interface out of them. It will only take methods (and properties) on Class2 and not inherited ones (otherwise you would get all public methods all the way down to your base class).

The solution would probably be to declare Class1 as a COM class also (it may be possible to mark it as private) then specify that the COM interfaceid of Class2 descends from the COM interfaceid of Class1.

Some thing like that shoudl do the trick.