Я получаю System.InvalidCastException при извлечении словаря из состояния сеанса - PullRequest
0 голосов
/ 16 июня 2011

[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]].Тип A происходит от «mscorlib, версия = 4.0.0.0, культура = нейтральная, PublicKeyToken = b77a5c561934e089» в контексте «LoadNeither» в местоположении

Синтаксис моего словаря:

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.String

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

Ответы [ 2 ]

1 голос
/ 16 июня 2011

Есть ли у вас две DisplayAllQuestionsTable версии этого типа?

Укажите имя типа 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>>;
1 голос
/ 16 июня 2011

Попытайтесь получить тип объекта Session ["ThreadPage"]:

Session["ThreadPage"].GetType().ToString();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...