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

时间:2011-06-16 07:31:06

标签: c# asp.net exception

  

[A] {System.Collections.Generic.Dictionary {1}} 1 [DisplayAllQuestionsTable]]   无法施展   [B] System.Collections.Generic.Dictionary 2[System.String,System.Collections.Generic.List 1 [DisplayAllQuestionsTable]。   A型来自'mscorlib,   版本= 4.0.0.0,文化=中立,   PublicKeyToken = b77a5c561934e089'in   位置上的上下文“LoadNeither”

我的字典语法:

2[System.String,System.Collections.Generic.List

当我尝试从Session中检索它时:

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

tPages = (Dictionary<string, List<DisplayAllQuestionsTable>>)Session["ThreadPage"]; // i get an exception here Unable to cast object of type [

,System.Collections.Generic.List System.String 2

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

您是否有这种类型的两个DisplayAllQuestionsTable版本?

使用命名空间限定DisplayAllQuestionsTable类型名称。

例如:

// 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>>;