Test if an object implements an interface

Solution 1:

Use TypeOf...Is:

If TypeOf objectParameter Is ISpecifiedInterface Then
    'do stuff
End If 

Solution 2:

I also found this article by Scott Hansleman to be particularly helpful with this. In it, he recommends


if (typeof(IWhateverable).IsAssignableFrom(myType)) { ... }

I ended up doing:


Dim _interfaceList As List(Of Type) = myInstance.GetType().GetInterfaces().ToList()
If _interfaceList.Contains(GetType(IMyInterface)) Then
   'Do the stuff
End If

Solution 3:


both requiredInterface and representedType are Types

Solution 4:

Here is a simple way to determine whether a given object variable "o" implements a specific interface "ISomething":

If o.GetType().GetInterfaces().Contains(GetType(ISomething)) Then
    ' The interface is implemented
End If