简体   繁体   English

从Session状态检索字典时,我得到System.InvalidCastException

[英]I get System.InvalidCastException, when retrieving a dictionary from Session state

[A]System.Collections.Generic.Dictionary 2[System.String,System.Collections.Generic.List 1[DisplayAllQuestionsTable]] cannot be cast to [B]System.Collections.Generic.Dictionary 2[System.String,System.Collections.Generic.List 1[DisplayAllQuestionsTable]]. [A] System.Collections.Generic.Dictionary 2[System.String,System.Collections.Generic.List 1 [DisplayAllQuestionsTable]]无法强制转换为[B] System.Collections.Generic.Dictionary 2[System.String,System.Collections.Generic.List 1 [DisplayAllQuestionsTable]]。 Type A originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 类型A源自'mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089',位于上下文'LoadNeither'中

My Dictionary syntax: 我的字典语法:

Dictionary<string, List<DisplayAllQuestionsTable>> tPages=tPages = 
   new Dictionary<string, List<DisplayAllQuestionsTable>>();

When i try to retrieve it from Session: 当我尝试从Session中检索它时:

tPages = (Dictionary<string, List<DisplayAllQuestionsTable>>)Session["ThreadPage"]; // i get an exception here

Unable to cast object of type [

System.String

,System.Collections.Generic.List 1[DisplayAllQuestionsTable]]' to type '**System.Collection**s.Generic.Dictionary 2 ,System.Collections.Generic.List 1[DisplayAllQuestionsTable]]' to type '**System.Collection**s.Generic.Dictionary 2

尝试获取Session [“ThreadPage”]对象的类型:

Session["ThreadPage"].GetType().ToString();

Do you have two different DisplayAllQuestionsTable versions of this type? 你有两种不同的DisplayAllQuestionsTable这种类型的版本吗?

Qualify the DisplayAllQuestionsTable type name with namespace. 使用命名空间限定DisplayAllQuestionsTable类型名称。

Ex: 例如:

// Creating the instance..
Dictionary<string, List<SampleAppNamespace.DisplayAllQuestionsTable>> tPages = 
   new Dictionary<string, List<SampleAppNamespace.DisplayAllQuestionsTable>>();

// putting it into session
Session["ThreadPage"] = tPages;

// reading back from session
tPages = Session["ThreadPage"] as Dictionary<string, List<SampleAppNamespace.DisplayAllQuestionsTable>>;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM