Exposing generic inherited methods to COM

Martin 21 Reputation points
2022-01-26T10:42:52.09+00:00

I'm developing a COM Visible DLL and try to re-use parts of my code by inheriting base classes. This works fine until I use a base class with a generic type. Although the generic type itself is never exposed it still doesn't work. I'm trying to think this is some sort of bug.

Here is an oversimplified version for testing:

Imports System.Runtime.InteropServices

Public MustInherit Class TestGeneric(Of T)
    Private _Value As T

    Public Sub DoSomething(Value As T)
        _Value = Value
    End Sub
End Class

<ClassInterface(ClassInterfaceType.AutoDual)>
Public Class TestString
    Inherits TestGeneric(Of String)
End Class

<ClassInterface(ClassInterfaceType.AutoDual)>
Public Class TestInteger
    Inherits TestGeneric(Of Integer)
End Class

After compiling I register the DLL and use it in VB6. In VB6 I can see both classes TestString and TestInteger but they do not have any methods. I would expect them to show TestString.DoSomething(Value as string) and TestInteger.DoSomething(Value as integer). These methods themselves do not contain generic types, they are just based on a generic type in the backend. Why would this these methods be exposed?

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,633 questions
{count} votes

Accepted answer
  1. RLWA32 42,001 Reputation points
    2022-01-26T18:03:08.617+00:00

    It may not be the most elegant solution but the following worked in my simple test -

    Imports System.Runtime.InteropServices
    
    
    <ComVisible(False)>
    Public MustInherit Class TestGeneric(Of T)
        Private _Value As T
    
        Public Sub DoSomething(Value As T)
            _Value = Value
        End Sub
    
        Public Function GetValue() As T
            Return _Value
        End Function
    End Class
    
    <InterfaceType(ComInterfaceType.InterfaceIsDual)>
    Public Interface ITestString
        Sub DoSomething(s As String)
        Function GetValue() As String
    End Interface
    
    <InterfaceType(ComInterfaceType.InterfaceIsDual)>
    Public Interface ITestInteger
        Sub DoSomething(i As Integer)
        Function GetValue() As Integer
    End Interface
    
    <ClassInterface(ClassInterfaceType.None)>
    Public Class TestString
        Inherits TestGeneric(Of String)
        Implements ITestString
    
        Private Sub ITestString_DoSomething(s As String) Implements ITestString.DoSomething
            MyBase.DoSomething(s)
        End Sub
        Private Function ITestString_GetValue() As String Implements ITestString.GetValue
            Return MyBase.GetValue()
        End Function
    End Class
    
    <ClassInterface(ClassInterfaceType.None)>
    Public Class TestInteger
        Inherits TestGeneric(Of Integer)
        Implements ITestInteger
    
        Private Sub ITestInteger_DoSomething(i As Integer) Implements ITestInteger.DoSomething
            MyBase.DoSomething(i)
        End Sub
        Private Function ITestInteger_GetValue() As Integer Implements ITestInteger.GetValue
            Return MyBase.GetValue()
        End Function
    End Class
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful