![](/img/trans.png)
[英]Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericBag'
[英]Unable to cast object from viewstate to collection
我在ViewState中存储List<KeyValuePair<string, bool>>
的集合,当我尝试将其投射回List<KeyValuePair<string, bool>>
错误状态时出现问题
无法转换类型为'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]]'。
那么如何从ViewState转换回我的键值列表呢?
我的代码行是:
List<KeyValuePair<string, bool>> myObject = (List<KeyValuePair<string, bool>>)ViewState["listOutputWords"];
尝试这个:-
KeyValuePair<string, bool> myObject = (KeyValuePair<string, bool>)ViewState["listOutputWords"];
正如编译器明确指出的那样,您的ViewState拥有通用的String,Bool
Dictionary
String,Bool
但是您试图将其强制转换为字典列表,这是错误的。
根据异常,您将在ViewState
保存Dictionary<string, bool>
。 如果您确实需要List<KeyValuePair<string, bool>>
,请尝试:
List<KeyValuePair<string, bool>> myObject = ((Dictionary<string, bool>)ViewState["listOutputWords"]).ToList();
或者,您也可以将其反序列化为字典并使用它:
Dictionary<string, bool> myObject = (Dictionary<string, bool>)ViewState["listOutputWords"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.