title: Unable to cast generic object
body: <p>I have the following conceptual model:</p>
  
  <pre><code>public interface IFoo&lt;out T&gt;
  {
      T Data { get; }
  }
  
  public struct Foo&lt;T&gt; : IFoo&lt;T&gt;
  {
      public Foo(T data) : this()
      {
          Data = data;
      }
  
      public T Data { get; private set; }
  }
  
  public class FooService&lt;T&gt; 
  {
      ...
  
      public Foo&lt;T&gt; Get(string id)
      {
          ...
      }
  }
  </code></pre>
  
  <p>Then I try to use it in a way that conceptually is equivalent to this:</p>
  
  <pre><code>// Create and register a few FooService instances
  ServiceLocator.Register(new FooService&lt;DateTime&gt;(), "someServiceId");
  ServiceLocator.Register(new FooService&lt;double?&gt;(), "anotherServiceId");
  
  // Retrieve a particular FooService instance and call the Get method
  var fooService = (FooService&lt;object&gt;)ServiceLocator.Get("someServiceId");
  var foo = fooService.Get("someFooId");
  </code></pre>
  
  <p>I want to use the Get() method on a FooService instance - no matter what type the selected FooService instance returns. However, this code results in the following exception:</p>
  
  <blockquote>
    <p>Unable to cast object of type 'WindowsFormsApplication7.FooService`1[System.DateTime]' to type 'WindowsFormsApplication7.FooService`1[System.Object]'.</p>
  </blockquote>
  
  <p>Any suggestions on how to solve this problem would be greately appreciated. </p>
  
  <p>You could argue, why I made the FooService generic in the first place. However, this is done to secure type safety in a type safe environment. In this particular case, though, the FooService shall be used in a Web API controller to serve various types of Foo. It shall return a response with Foo of T disregarding the type of T.</p>