How do I know if an interface type implements?

I need to know if the Type interface implements the interface.

  Dim asmRule As System.Reflection.Assembly = System.Reflection.Assembly.LoadFrom(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Rule.dll")) For Each typeAsm As System.Type In asmRule.GetTypes If TypeOf typeAsm Is Rule.IRule Then 'that does always return false even though they implement IRule' End If Next 

Thanks to everyone. Now I know why typeof did not work. The type, of course, does not implement IRule. I have filtered two options from your answers:

  • GetType(Rule.IRule).IsAssignableFrom(typeAsm)
  • typeAsm.GetInterface(GetType(Rule.IRule).FullName) IsNot Nothing

What is the best performance choice?

UPDATE : I found out that it is better to use:

 Not typeAsm.GetInterface(GetType(Rule.IRule).FullName) Is Nothing 

instead

 GetType(Rule.IRule).IsAssignableFrom(typeAsm) 

because the IRule interface itself can be assigned to IRule, which calls MissingMethodExcpetion if I try to instantiate:

 ruleObj = CType(Activator.CreateInstance(typeAsm, True), Rule.IRule) 

UPDATE2: Thanks to Ben Voigt. He convinced me that IsAssignableFrom in combination with IsAbstract might be the best way to check if a given type of interface implements the interface and not the interface itself (which raises a MissingMethodException if you are trying to instantiate).

 If GetType(Rule.IRule).IsAssignableFrom(typeAsm) AndAlso Not typeAsm.IsAbstract Then 
+6
inheritance c # typechecking
source share
5 answers

TypeOf ... Is works great with inheritance:

 Imports System Public Class Test Public Shared Sub Main() Dim o As Object = 5 If TypeOf o Is IFormattable Then Console.WriteLine("Yes") End If End Sub End Class 

This works the same as it does in C #.

However, in your example, to try to determine if System.Type (or any other subclass) is Rule.IRule using Rule.IRule . This will not work ... because you are not interested in whether the System.Type interface is implemented, you are wondering if the System.Type instance refers to the typeAsm variable in the typeAsm implementation.

In other words, your code would not improve in C #. You need Type.IsAssignableFrom when talking about an instance of System.Type :

 If GetType(Rule.IRule).IsAssignableFrom(typeAsm) Then 
+13
source share

In C #:

 x is Class1 

in VB.NET:

 TypeOf x Is Class1 

Described here.

+5
source share

Another option:

 If myType.GetInterface("Rule.IRule") IsNot Nothing Then 

http://www.hanselman.com/blog/DoesATypeImplementAnInterface.aspx

+3
source share

You are using the wrong keyword.

The C # is keyword and VB.Net TypeOf ... Is ... will check if the instance has a type that implements the interface.
Thus, your code checks to see if the typeAsm variable, which is an instance of System.Type , implements the IRule interface.
Since the System.Type class does not implement IRule , it will always return False .

You are actually trying to check if the type represented by the System.Type IRule instance IRule .
To do this, you need to use the methods in the System.Type class.

For example:

  If typeAsm.GetInterface(GetType(Rule.IRule)) IsNot Nothing Then 
+3
source share
+2
source share

All Articles