title: ASP.NET cannot cast session variable in same type variable
body: <p>i'm having some "almost random" error while trying to cast a session variable to it's belonging variable type.
  Just to make it clear:</p>
  
  <p>i have a class "Elemento", i just create an istance of it and place it in my session variable:</p>
  
  <pre><code>Elemento elem = new Elemento(id, quantity);
  list.Add(elem);
  context.Session["cart"] = list;
  </code></pre>
  
  <p>Now i have to recover my list, and i try to do it with:</p>
  
  <pre><code>list = (List&lt;Elemento&gt;)context.Session["cart"];
  </code></pre>
  
  <p>Well.. this "sometime" works, sometime not! The first time i tried it it worked flawlessy, but now i have "internal server error 500" with this error:</p>
  
  <blockquote>
    <p>Impossibile eseguire il cast di
    [A]System.Collections.Generic.List<code>1[Elemento] a
    [B]System.Collections.Generic.List</code>1[Elemento]. Il tipo A ha origine
    da 'mscorlib, Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=b77a5c561934e089' nel contesto 'LoadNeither' nella
    posizione
    'C:\Windows\assembly\GAC_64\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll'.
    Il tipo B ha origine da 'mscorlib, Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=b77a5c561934e089' nel contesto 'LoadNeither' nella
    posizione
    'C:\Windows\assembly\GAC_64\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll'.</p>
  </blockquote>
  
  <p>translating it..</p>
  
  <blockquote>
    <p>Cannot cast [A]System.Collections.Generic.List<code>1[Elemento] to
    [B]System.Collections.Generic.List</code>1[Elemento]. Type A have origin
    from 'mscorlib, Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=b77a5c561934e089' with context 'LoadNeither' and
    position
    'C:\Windows\assembly\GAC_64\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll'.
    Type B have origin from 'mscorlib, Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=b77a5c561934e089' with context 'LoadNeither' and
    position
    'C:\Windows\assembly\GAC_64\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll'.</p>
  </blockquote>
  
  <p>i can solve this "for some time" emptying the IIS cache.. but it will happen again when i build the solution from visual studio.</p>
  
  <p>i read in some place i can solve this using interfaces.. but since i'm still learning how to use them i can't try directly for now, there is some tested solution to this?</p>
  
  <p>EDIT:
  Works with krshekhar solution:</p>
  
  <pre><code>list = context.Session["cart"] as List&lt;Elemento&gt;;
  </code></pre>
  
  <p>thank you!</p>