jeudi 4 février 2016

Get all class types that implement a specific interface using VB.Net

I want to load all Forms that implement the interface IFormLoadSubscriber.

Interface

Namespace Interfaces
    Public Interface IFormLoadSubscriber

    End Interface
End Namespace

At this time it doesn't add anything, subscribing to it is enough.

Form

Namespace Forms
    Public Class MainForm
        Inherits Base.Base
        Implements IFormLoadSubscriber

    End Class
End Namespace

That Base.Base is a form that enforces base behavior.

What I have

Private Shared Function GetSubscribers() As List(Of Type)
    Dim type = GetType(IFormLoadSubscriber)
    Dim subscribers = AppDomain.CurrentDomain.GetAssemblies() _
                      .Where(Function(x) type.IsAssignableFrom(type)) _
                      .Select(Function(x) x.GetTypes())

    Return subscribers
End Function

The problem

The above code does not work as expected, because it returns a large list of lists, containing all sorts of types. If mine is included, it's impossible to find manually. At any rate, this is not what I need.

The question

How do I change the above code so that it returns only one class (as only one class implements IFormLoadSubscriber), in this case my MainForm?





Aucun commentaire:

Enregistrer un commentaire