title: C# Factory Method with Generic Cast to Interface
body: <p>I have the following classes:</p>
  
  <pre><code>// -- model hierarchy
  public interface IJob {
  }
  
  public abstract class AbstractJob : IJob {
  }
  
  public class FullTimeJob : AbstractJob {               
  }
  
  // -- dao hierarchy
  public interface IJobDao&lt;T&gt; where T : IJob {       
    T findById(long jobId);
    long insert(T job);
  }
  
  public interface IFullTimeJobDao : IJobDao&lt;FullTimeJob&gt; {        
  }
  
  public abstract class AbstractDao {    
  }
  
  public abstract class AbstractJobDaoImpl&lt;T&gt; : AbstractDao, IJobDao&lt;T&gt; where T : IJob {
    public T findById(long jobId) {
      // omitted for brevity
    }
  
    public long insert(T job) {
      // omitted for brevity
    }
  }
  
  public class FullTimeJobDaoImpl : AbstractJobDaoImpl&lt;FullTimeJob&gt;, IFullTimeJobDao {
  }
  </code></pre>
  
  <p>I'm calling the following code from a factory method, which does not seem to work:</p>
  
  <pre><code>public IJobDao&lt;IJob&gt; createJobDao(long jobDaoTypeId)
  {
      object jobDao = Activator.CreateInstance(typeof(FullTimeJobDaoImpl));
      return jobDao as IJobDao&lt;IJob&gt;; // &lt;-- this returns null
      return (IJobDao&lt;IJob&gt;) jobDao; // &lt;-- this cast fails
  }
  </code></pre>
  
  <p>How is this "up cast" properly achieved?</p>