title: C# Casting object with inner generic
body: <p>I'm trying to cast a concrete class with generics to its interfaces and getting an error. I'm not sure what I'm missing. I need to get them into the common type.</p>
  
  <blockquote>
    <p>Unable to cast object of type 'HierarchalLocationFilterSet' to type 'ImAFilterSet`1[ModelBase]'.</p>
  </blockquote>
  
  <p>Everything compiles and runs, I just cant seem to do something like below.</p>
  
  <pre><code>ImAFilterSet&lt;IModelBase&gt; c = new HierarchalLocationFilterSet()
  </code></pre>
  
  <p>I can go half way ok</p>
  
  <pre><code>var thisWorks = c as FilterSetBase&lt;LocationModel&gt;;
  var thisAlsoWorks = c as ImAFilterSet&lt;LocationModel&gt;;
  //This is a hard fail   
  var x = ((ImAFilterSet&lt;ModelBase&gt;) c );
  var y = ((ImAFilterSet&lt;IModelBase&gt;) c );
  </code></pre>
  
  <p>Given</p>
  
  <pre><code>public interface ImAFilterSet&lt;C&gt; where C : IModelBase
  {
       List&lt;C&gt; Children { get; set; }
  }
  
  public abstract class FilterSetBase&lt;C&gt; : ImAFilterSet&lt;C&gt; where C : IModelBase
  
  public class HierarchalLocationFilterSet : FilterSetBase&lt;LocationModel&gt;
  
  public class LocationModel : ModelBase
  
  public abstract class ModelBase : IModelBase
  </code></pre>
  
  <p>Thanks!</p>