title: c# Generic Type System - Self Reference Generic Type
body: <p>Hey so I am trying to create a set of Generics and I cant seem to find the proper way to do this.</p>
  <p>What I need is better showcased as an example implementation (which doesnt work):</p>
  <pre class="lang-cs prettyprint-override"><code>public abstract class State&lt;T,K&gt; : StateMachine&lt;StateType&gt;{
  public T Owner { get;set}
  public Y Controller { get;set}
  
  public void Setup(T Owner, Y Controller);
  
  }
  
  
  public abstract class StateMachine&lt;StateType&gt; where StateType :  State&lt;T,K&gt;
  where K  :  &quot;MyOwnImplementationType typeof&lt;this&gt;??&quot;// This refers to the &quot;this&quot; concrete type and not a special provided type, it since I cant seem to constraint to be the correct type, and inheritance type does not work
   {
  
  StateType[] states; // This needs to be the actual StateType and not the abstract class
  
  public DoStuff(StateType t){
   t.Setup(this, GetOwner()); // doesnt matter where Owner Comes from
  }
  }
  
  
  public class ImplementationState :  State&lt;ImplementationStateMachine, ImplementationController&gt; {
  }
  
  
  public class ImplementationStatemachine :  StateMachine&lt;ImplementationState, ImplementationController&gt; {
  }
  
  </code></pre>
  <p>If I replace MyOwnType with  StateMachine, then it complains, and correctly, that my concrete implementation cant be mapped to the generic type.</p>
  <p>I have been playing around with using interfaces and CoVariance/ ContraVariance but I cant seem to get the correct setup  at all.</p>
  <p>Is there anyway this is possible ?
  Or any workaround how to setup this?
  I am open to suggestions on another way to structure this sort of system.</p>
  <p>The requirements is that I am able to access the correct types inside each class and not a generic variant.</p>