[英]Unable to cast object from viewstate to collection
I'm storing a collection of List<KeyValuePair<string, bool>>
in ViewState, the problem occurs when i'm trying to cast it back to List<KeyValuePair<string, bool>>
Error states 我在ViewState中存储
List<KeyValuePair<string, bool>>
的集合,当我尝试将其投射回List<KeyValuePair<string, bool>>
错误状态时出现问题
Unable to cast object of type 'System.Collections.Generic.Dictionary
2[System.String,System.Boolean]' to type 'System.Collections.Generic.List
1[System.Collections.Generic.KeyValuePair`2[System.String,System.Boolean]]'.无法转换类型为'System.Collections.Generic.Dictionary
2[System.String,System.Boolean]' to type 'System.Collections.Generic.List
1 [System.Collections.Generic.KeyValuePair`2 [System.String ,System.Boolean]]'。
So how can convert my key value list back from the ViewState ? 那么如何从ViewState转换回我的键值列表呢?
My code line is: 我的代码行是:
List<KeyValuePair<string, bool>> myObject = (List<KeyValuePair<string, bool>>)ViewState["listOutputWords"];
Try this:- 尝试这个:-
KeyValuePair<string, bool> myObject = (KeyValuePair<string, bool>)ViewState["listOutputWords"];
As clearly mentioned by the compiler, your ViewState is holding a generic Dictionary
of String,Bool
but you are trying to cast it to List of Dictionary which is wrong. 正如编译器明确指出的那样,您的ViewState拥有通用的
String,Bool
Dictionary
String,Bool
但是您试图将其强制转换为字典列表,这是错误的。
Based on the exception, you're saving a Dictionary<string, bool>
in ViewState
. 根据异常,您将在
ViewState
保存Dictionary<string, bool>
。 If you really need a List<KeyValuePair<string, bool>>
, try: 如果您确实需要
List<KeyValuePair<string, bool>>
,请尝试:
List<KeyValuePair<string, bool>> myObject = ((Dictionary<string, bool>)ViewState["listOutputWords"]).ToList();
Or you could just deserialize it to a dictionary and use that: 或者,您也可以将其反序列化为字典并使用它:
Dictionary<string, bool> myObject = (Dictionary<string, bool>)ViewState["listOutputWords"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.