title: How to see if a type implements an interface?
body: <p>I need to know if a <code>Type</code> implements an interface.  </p>
  
  <pre><code> 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
  </code></pre>
  
  <p>Thanks to all. Now i know why typeof didn't work. The type naturally does not implement IRule. I have filtered out two options from your answers:</p>
  
  <ol>
  <li><code>GetType(Rule.IRule).IsAssignableFrom(typeAsm)</code></li>
  <li><code>typeAsm.GetInterface(GetType(Rule.IRule).FullName) IsNot Nothing</code></li>
  </ol>
  
  <p>What is the better choise according to performance?</p>
  
  <p><strong>UPDATE</strong>: i've found out that it might be better to use:</p>
  
  <pre><code>Not typeAsm.GetInterface(GetType(Rule.IRule).FullName) Is Nothing
  </code></pre>
  
  <p>instead of </p>
  
  <pre><code>GetType(Rule.IRule).IsAssignableFrom(typeAsm)
  </code></pre>
  
  <p>because the Interface IRule itself is assignable of IRule what raises a MissingMethodExcpetion if i try to create an instance:</p>
  
  <pre><code>ruleObj = CType(Activator.CreateInstance(typeAsm, True), Rule.IRule)
  </code></pre>
  
  <p><strong>UPDATE2:</strong>
  Thanks to Ben Voigt. He convinced me that IsAssignableFrom in combination with IsAbstract might be the best way to check if a given type implements an interface and <strong>is not</strong> the interface itself (what throws a MissingMethodException if you  try to create an instance).</p>
  
  <pre><code>If GetType(Rule.IRule).IsAssignableFrom(typeAsm) AndAlso Not typeAsm.IsAbstract Then
  </code></pre>