title: 'InvalidCastException' while trying to cast concrete object to generic interface
body: <p>I'm trying to create a class using Activator.CreateInstance and cast it to a generic interface it implements.</p>
  
  <p>Why am I getting 'InvalidCastException' at run time (.Net 4.0)?</p>
  
  <p>Here is the code:</p>
  
  <pre><code>namespace CastingGenerics
  {
      class Program
      {
          static void Main(string[] args)
          {
              var typeFromSomewhere = typeof(ActionSomethingValidator);
  
              // This line throw 'System.InvalidCastException'
              // "Unable to cast object of type 'CastingGenerics.ActionSomethingValidator' to type 'CastingGenerics.IActionValidator`1[CastingGenerics.IAction]'."
              var castingError = (IActionValidator&lt;IAction&gt;)Activator.CreateInstance(typeFromSomewhere);
          }
      }
  
      internal interface IAction
      {
      }
  
      internal interface IActionValidator&lt;in T&gt;
          where T : IAction
      {
          bool Validate(T action, out string failure);
      }
  
      internal abstract class ActionValidator&lt;T&gt; : IActionValidator&lt;T&gt;
          where T : IAction
      {
          public abstract bool Validate(T action, out string failure);
      }
  
      internal class ActionSomethingValidator : ActionValidator&lt;IActionSomething&gt;
      {
          public override bool Validate(IActionSomething action, out string failure)
          {
              failure = string.Empty;
              return true;
          }
      }
  
      internal interface IActionSomething : IAction
      {
      }
  }
  </code></pre>