title: How to cast into generic base class
body: <p>I have few classes which inherits from a generic class like <code>FooBasePolicy&lt;TFooTarget&gt;</code>. And I want to cast my derived classes by their base class (not sure if its right way to say it). My classes are:</p>
  
  <pre><code>public class FooTarget{}
  
  public class GTeamTarget : FooTarget{}
  
  public class MTeamTarget : FooTarget{}
  
  public class FooBasePolicy{}
  
  public class FooBasePolicy&lt;TFooTarget&gt; : FooBasePolicy where TFooTarget : FooTarget
  {
      public virtual TFooTarget SomeFooTarget { get;set; }
  }
  
  public class GTeamPolicy : FooBasePolicy&lt;GTeamTarget&gt;
  {
      public GTeamPolicy()
      {
          SomeFooTarget = new GTeamTarget();
      }
  }
  
  public class MTeamPolicy : FooBasePolicy&lt;MTeamTarget&gt;
  {
      public GTeamPolicy()
      {
          SomeFooTarget = new MTeamTarget();
      }
  }
  </code></pre>
  
  <p>And then I tried to use these this way, </p>
  
  <p>problem is I don't know upfront which TeamTarget it is. It can be G or M.</p>
  
  <pre><code>FooBasePolicy&lt;FooTarget&gt; policy = null;
  if (something.Equals("GTEAM"))
  {
      //This gives me an implicit conversion error. It can't cast.
      policy = FromBinary(type, funnelData.Data) as FooBasePolicy&lt;FooTarget&gt;;
  }
  else if (something.Equals("MTEAM"))
  {
      policy = FromBinary(type, funnelData.Data) as FooBasePolicy&lt;FooTarget&gt;;
  }
  </code></pre>
  
  <p>FromBinary actually returns a deserialized object.</p>
  
  <p>But it perfectly can cast into <code>FooBasePolicy</code>. But then I miss <code>SomeFooTarget</code> property.</p>
  
  <p>Any idea? Please help.</p>