title: Force C# Cast To Generic
body: <p>There is a tool I'm modifying as I want to be able to use a generic type.
  It has properties "AsDouble", "AsBool", etc.</p>
  
  <p>I want to force a typecast and I don't care if it throws in runtime. I just want it to compile.</p>
  
  <p>So... this does not compile...</p>
  
  <pre><code>public T As&lt;T&gt;()
  {
      Type type = typeof(T);
      if (type == typeof(Double))
      {
          return (T)(AsDouble);
      }
      // more conditionals
  }
  </code></pre>
  
  <p>Yet this does...</p>
  
  <pre><code>public T As&lt;T&gt;()
  {
      Type type = typeof(T);
      if (type == typeof(Double))
      {
          return (T)(AsDouble as object);
      }
      // more conditionals
  }
  </code></pre>
  
  <p>Why is it acting like this? How do I make it stop complaining and just do what I ask it too? Or is there a much better way of doing this than what I have in mind?</p>
  
  <p>// Not duplicate of the one suggested. (Type1)Type2 does not work... I am wondering why converting from T1 to T2 to T3 works but not T1 to T3. (T1 being double, T2 being object, T3 being generic type.) Please read my question before flagging it as a duplicate.</p>