none
Overriding overloaded method names when exposing to COM RRS feed

  • Question

  •  

    Hi, I've got a .NET class that I've been asked to expose to COM which I've gone through the steps and all works just fine with one cosmetic gripe. 

     

    When you expose the class to COM it auto-appends overloaded method names with _n (where n is the number of overloads starting at 2 as described in the documentation)I've been looking for a way to explicitly override these auto-generated names without affecting any .NET applications already use this class.  The only method by which I've been able to manage this I find a little ugly and was hoping someone could provide a more elegant solution:

    Code Snippet

    'Example of scenario the original class

    <ComClass(MyConnectorClass.ClassId, MyConnectorClass.InterfaceId, MyConnectorClass.EventsId)> _

    Public Class MyConnectorClass

     

      Public Const ClassId As String = "DC477A47-0453-4c27-84F8-C4E462475840"

      Public Const InterfaceId As String = "E33A80A0-884A-4911-9155-0F714DBF0B93"

      Public Const EventsId As String = "CF6A5B16-21B4-42c4-8A8D-3AF6C3675FBE"

     

      Private _Address As String = "localhost" 'connect to localhost if no address is provided

     

      Public Property Address() As String

        Get

          Return _Address

        End Get

        Set(ByVal value As String)

          _Address = value

        End Set

      End Public Property

     

      Public Event Connected(ByVal Sender As Object, ByVal e As System.EventArgs)

      Public Event Disconnected(ByVal Sender As Object, ByVal e As System.EventArgs)

     

      Public Function Connect() As Boolean

        Return Connect(_Address)

      End Function

     

      Public Function Connect(ByVal Address As String) As Boolean

        Try

          '....do stuff

     

          RaiseEvent Connected(Me, New System.EventArgs)

          Return True

     

        Catch Ex As ConnectionException

          Throw

     

        End Try

     

      End Function

     

      Public Function Disconnect() As Boolean

        Try

          '....do stuff

     

          RaiseEvent Disconnected(Me, New System.EventArgs)

          Return True

     

        Catch Ex As ConnectionException

          Throw

     

        End Try

     

      End Function

     

    End Class

     

    It exposes to COM as it appears it should thus:

    Code Snippet

    Dim oConnect As MyConnectorClass

    Set oConnect = New MyConnectorClass

     

    oConnectorClass.Connect()

    'OR

    oConnectorClass.Connect_2("192.168.1.102")

     

    '....Do other stuff

     

    oConnectorClass.Disconnect()

     

     

    Is there any way of specifying that Connect_2 should be ConnectEx using some attribute or other?  The only method I've been able to come up with isn't exactly what I would call elegant:

    Code Snippet

    <ComVisible(True)> _

    Public Function Connect() As Boolean

      Return Connect(_Address)

    End Function

     

    <ComVisible(True)> _

    Public Function ConnectEx(ByVal Address As String) As Boolean

      Return Connect(Address)

    End Function

     

    <ComVisible(False)> _

    Public Function Connect(ByVal Address As String) As Boolean

      '....do stuff

    End Function

     

    This would mean that anyone referencing the class in .NET now sees the ConnectEx method which is really only relevant for applications accessing it through COM.

     

    It would be far more elegant if I could use the concept described by:

    Code Snippet

    <ComVisible(True)> _

    Public Function Connect() As Boolean

      Return Connect(_Address)

    End Function

     

    <ComVisible(True), ExposeAs("ConnectEx")> _

    Public Function Connect(ByVal Address As String) As Boolean

      '....do stuff

    End Function

     

    Any ideas anyone?

    Tuesday, April 8, 2008 5:32 PM

Answers

  • COM doesn't support overloading method names.

     

    The only way you can do this is to define an interface for COM clients.  You should:

    - Define a ComVisible interface (e.g. IMyConnectorClass) for your COM clients with the method names you want

    - Add the attribute ClassInterface(ClassInterfaceType.None) to prevent generation of a class interface for your class

    - Inherit IMyConnectorClass in your class

    - Explicitly implement IMyComConnectorClass

     

    Something like the following (I use C# so the syntax might not be accurate):

     

    Code Snippet

    <ComClass(MyConnectorClass.ClassId, MyConnectorClass.InterfaceId, MyConnectorClass.EventsId), ClassInterface(ClassInterfaceType.None)> _

    Public Class MyConnectorClass Implements IMyConnectorClass

     

        Public Function Connect(ByVal Address As String) As Boolean

            ... do stuff

        End Function

     

        ' For .NET clients

        Public Function Connect() As Boolean

            Return Connect(_Address)

        End Function

     

        ' For COM clients via IMyConnectorClass interface

        Private Function ConnectEx Implements IMyConnectorClass.ConnectEx

            Return Connect()

        End Function

     

    End Class

     

    <ComVisible(True)>

    Public Interface IMyConnectorClass

       Function Connect(ByVal Address As String) As Boolean

     

       Function ConnectEx() As Boolean

     

    End Interface

     

     

     

    Wednesday, April 9, 2008 6:45 AM

All replies

  • Thank you for your post.  I have moved this post to the .NET Development / Common Language Runtime forum for visibility.

    Tuesday, April 8, 2008 9:34 PM
    Moderator
  • COM doesn't support overloading method names.

     

    The only way you can do this is to define an interface for COM clients.  You should:

    - Define a ComVisible interface (e.g. IMyConnectorClass) for your COM clients with the method names you want

    - Add the attribute ClassInterface(ClassInterfaceType.None) to prevent generation of a class interface for your class

    - Inherit IMyConnectorClass in your class

    - Explicitly implement IMyComConnectorClass

     

    Something like the following (I use C# so the syntax might not be accurate):

     

    Code Snippet

    <ComClass(MyConnectorClass.ClassId, MyConnectorClass.InterfaceId, MyConnectorClass.EventsId), ClassInterface(ClassInterfaceType.None)> _

    Public Class MyConnectorClass Implements IMyConnectorClass

     

        Public Function Connect(ByVal Address As String) As Boolean

            ... do stuff

        End Function

     

        ' For .NET clients

        Public Function Connect() As Boolean

            Return Connect(_Address)

        End Function

     

        ' For COM clients via IMyConnectorClass interface

        Private Function ConnectEx Implements IMyConnectorClass.ConnectEx

            Return Connect()

        End Function

     

    End Class

     

    <ComVisible(True)>

    Public Interface IMyConnectorClass

       Function Connect(ByVal Address As String) As Boolean

     

       Function ConnectEx() As Boolean

     

    End Interface

     

     

     

    Wednesday, April 9, 2008 6:45 AM