title: Checking for object type compatibility at runtime
body: <p>This is a pretty general question, but the specific thing I'm doing is simple so I'm including the code. How do I check for type compatibility between two objects when I don't know the type of either at compile time?</p>
  
  <p>That is, I can do <code>if (object is SomeType)</code> when <code>SomeType</code> is a type name known at compile time. <code>GetType()</code> is not sufficient because it will not work with derived types. Basically I want to be able to say, <code>if (object.IsTypeOfOrIsDerivedFrom(someType))</code> where sig of this magical method is <code>IsTypeOfOrIsDerivedFrom(Type type)</code></p>
  
  <p>Here is the context.</p>
  
  <pre><code>// Return all controls that are (or are derived from) any of a list of Types
  public static IEnumerable&lt;Control&gt; FindControls(this Control control, IEnumerable&lt;Type&gt; types, bool recurse) 
  {
      foreach (Control ctl in control.Controls)
      {
          /// How can I compare the base types of item &amp; ctl?
          if (types.Any(item=&gt;  .... ))
          {
              yield return (ctl);
          }
          if (recurse &amp;&amp; ctl.Controls.Count &gt; 0)
          {
              IEnumerable&lt;Control&gt; subCtl = ctl.FindControls(types,true);
              if (subCtl != null)
              {
                  yield return (subCtl);
              }
          }
      }
      yield break;
  }
  </code></pre>