I have an "almost random" error while trying to pass a session variable belonging to a type variable. Just to make it clear:
I have an Elemento class, I just create it and put it in my session variable:
Elemento elem = new Elemento(id, quantity); list.Add(elem); context.Session["cart"] = list;
Now I need to restore the list, and I'm trying to do this with
list = (List<Elemento>)context.Session["cart"];
Well .. it "someday" works, someday it doesn't! The first time I tried it, it worked flawlessly, but now I have a "500 internal server error" with this error:
Imposition [A] System.Collections.Generic.List 1[Elemento] a [B]System.Collections.Generic.List 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____77775c561934l089 . Il tipo B ha origine da 'mscorlib, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089' nel contesto 'LoadNeither' nella POSIZIONE 'C: \ Windows \ build \ GAC_64 \ mscorlib \ 2.0.0.0__b77a5c561934eib. dll '.
translate it.
Cannot use [A] System.Collections.Generic.List 1[Elemento] to [B]System.Collections.Generic.List 1 [Elemento]. Type A is derived from 'mscorlib, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089' with the context "LoadNeither" and the position "C: \ Windows \ assembly \ GAC_64 \ mscorlib \ 2.0.0.0__b77a5c561934e089 \ mscorlib.dll. Type B has a start from 'mscorlib, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089' with the context 'LoadNeither' and the position 'C: \ Windows \ assembly \ GAC_64 \ mscorlib \ 2.0.0.0__b77a5c561934e089 \ mscorlib.dll' .
I can solve this βfor a whileβ by emptying the IIS cache, but it will happen again when I build the solution from visual studio.
I am reading in some place, I can solve it using interfaces .. but since I'm still learning how to use them, I canβt try right now, is there any proven solution for this?
EDIT: Works with krshekhar solution:
list = context.Session["cart"] as List<Elemento>;
Thank you!