Home

VB.NET: List Classes That Inherit Interface

Posted by SteveHardie | On: May 19 2011

I want to be able to create a list of all classes that inherit a specified interface within a given namespace.

Here is a quick bit of code that will give you a list of System.Type wich will contain all classes that inherit the specified interface.

Import the System.Reflection Namespace

Imports System.Reflection

'// Response
            Dim Resp As New Generic.List(Of System.Type)

            '// Gets the Assembly
            Dim MyAssembly As [Assembly]
            MyAssembly = [Assembly].GetAssembly(GetType(~interface or class in namespace~))

            '// Traverse all Types
            For Each t As System.Type In MyAssembly.GetTypes()
                If t.GetInterface(GetType(~inherited interface~).ToString) IsNot Nothing Then
                    Resp.Add(t)
                End If
            Next

You will be left with a System.Type list containing classes that inherit the specified interface.

Leave a comment