Ani*_*nil 6 c# serialization hashtable
我已经实现了会话状态模式sqlserver,当我运行我的应用程序时,我面临着哈希表的XML序列化错误.我的班级看起来像:
[Serializable]
public class ProjectSetup{
private System.Collections.Hashtable _ConfigTable;
//and other properties here
public System.Collections.Hashtable ConfigTable
{
get { return _ConfigTable; }
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想知道如何序列化hastable或如果有另一种选择请告诉我.
并且确切的错误是:"无法序列化System.Collections.Hashtable类型的成员ProjectSetup.ConfigTable,因为它实现了IDictionary"
一种方法是在您的类上实现 IXmlSerialized 并手动序列化哈希表。请参阅这篇文章了解更多详细信息。
public void WriteXml(System.Xml.XmlWriter writer)
{
// Used while Serialization
// Serialize each BizEntity this collection holds
foreach( string key in this.Dictionary.Keys )
{
Serializer.Serialize(writer, this.Dictionary[key]);
}
}
public void ReadXml(System.Xml.XmlReader reader)
{
// Used while Deserialization
// Move past container
reader.Read();
// Deserialize and add the BizEntitiy objects
while( reader.NodeType != XmlNodeType.EndElement )
{
BizEntity entity;
entity = Serializer.Deserialize(reader) as BizEntity;
reader.MoveToContent();
this.Dictionary.Add(entity.Key, entity);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13214 次 |
| 最近记录: |