如何序列化/反序列化一个字典变量? [英] How to Serialize/DeSerialize a Dictionary variable?
问题描述
我有一个字典变量,程序读取XML文件,并然后实例按类型对性能存储在字典可变对象。我想存储字典变量的memcache重用,但是,因为字典变量,实例化的对象是引用类型,当我使用实例化对象来改变一些值,memcache中的缓存值也发生了变化。
code如下所示。 类的字典变量,XPathNavigator的变量不能序列化。我如何序列化/反序列化或达到类似的效果?谢谢你。
命名空间ObjectReference
{
公共接口IMyObject
{
INT标识{获取;组; }
字符串URL {获得;组; }
布尔州{获得;组; }
布尔SetItemXml(XPathNavigator的导航器);
}
[可序列化]
公共类myLink的:IMyObject
{
公众诠释编号{获得;组; }
公共字符串URL {获得;组; }
公共布尔州{获得;组; }
私人的XPathNavigator _xmlNavigator;
公共BOOL SetItemXml(XPathNavigator的导航器)
{
_xmlNavigator = navigator.Clone();
n = int.Parse(_xmlNavigator.SelectSingleNode(ID)值);
URL = _xmlNavigator.SelectSingleNode(URL)的价值。
返回true;
}
}
[可序列化]
公共类MyPicture:IMyObject
{
公众诠释编号{获得;组; }
公共字符串URL {获得;组; }
公共布尔州{获得;组; }
私人的XPathNavigator _xmlNavigator;
公共BOOL SetItemXml(XPathNavigator的导航器)
{
_xmlNavigator = navigator.Clone();
n = int.Parse(_xmlNavigator.SelectSingleNode(ID)值);
URL = _xmlNavigator.SelectSingleNode(URL)的价值。
返回true;
}
}
公共部分类_Default:System.Web.UI.Page
{
公众的IDictionary<字符串的IDictionary< INT,IMyObject>> CreateObjects()
{
IDictionary的<字符串的IDictionary< INT,IMyObject>>对象=新字典<字符串的IDictionary< INT,IMyObject>>();
VAR读者= XmlTextReader的新(新StringReader(@&LT; XML版本=1.0 encoding='utf-8'?><root><item><type>MyLink</type><id>1</id><url>http://www.google.com</url></item><item><type>MyLink</type><id>2</id><url>http://stackoverflow.com</url></item><item><type>MyPicture</type><id>3</id><url>http://static.adzerk.net/Advertisers/2565.png</url></item></root>"));
XPathNavigator的导航=新的XPathDocument(读者).CreateNavigator();
声明XPathNodeIterator节点= navigator.Select(//根/项目);
而(nodes.MoveNext())
{
。字符串classType所= nodes.Current.SelectSingleNode(类)的价值;
INT ID = int.Parse(nodes.Current.SelectSingleNode(ID)值);
如果(!objects.ContainsKey(classType所)||!反对[classType所] .ContainsKey(ID))
{
IMyObject myObject的= Activator.CreateInstance(Type.GetType(string.Concat(ObjectReference,classType所)))为IMyObject;
myObject.SetItemXml(nodes.Current);
如果(!objects.ContainsKey(classType所))
objects.Add(classType所,新的字典&LT; INT,IMyObject&GT;(){{ID,myObject的}});
否则,如果(!对象[classType所] .ContainsKey(ID))
对象[classType所]。新增(ID,myObject的);
}
}
返回对象;
}
保护无效的Page_Load(对象发件人,EventArgs的)
{
IDictionary的&LT;字符串的IDictionary&LT; INT,IMyObject&GT;&GT;链表=新字典&LT;字符串的IDictionary&LT; INT,IMyObject&GT;&GT;();
如果(HttpContext.Current.Cache [ObjectCache]!= NULL)
{
链表=(词典&LT;字符串的IDictionary&LT; INT,IMyObject&GT;&GT;)HttpContext.Current.Cache [ObjectCache];
}
其他
{
链表= CreateObjects();
HttpContext.Current.Cache.Insert(
ObjectCache
链表,
空值,
DateTime.Now.AddMinutes(2),
System.Web.Caching.Cache.NoSlidingExpiration);
}
的foreach(在链表VAR父)
{
的foreach(VAR孩子链表[parent.Key])
{
如果(假== child.Value.State)
{
// TODO ...注意,在这里
child.Value.State = TRUE;
}
}
}
}
}
}
有一个在类似的 XML序列化通用词典
I have a Dictionary variable, the program reads XML files, and then instantiates objects stored in the Dictionary variable by type for performance. I would like to store the Dictionary variable to memcache for reuse, but, because the Dictionary variable and instantiated objects are reference types, when I operate instantiated objects to change some value, the cache value of memcache also changed.
Code like the following. Dictionary variable and XPathNavigator variable of class can't serialize. How can I Serialize/DeSerialize or achieve a similar effect? Thanks.
namespace ObjectReference
{
public interface IMyObject
{
int Id { get; set; }
string Url { get; set; }
bool State { get; set; }
bool SetItemXml(XPathNavigator navigator);
}
[Serializable]
public class MyLink : IMyObject
{
public int Id { get; set; }
public string Url { get; set; }
public bool State { get; set; }
private XPathNavigator _xmlNavigator;
public bool SetItemXml(XPathNavigator navigator)
{
_xmlNavigator = navigator.Clone();
Id = int.Parse(_xmlNavigator.SelectSingleNode("id").Value);
Url = _xmlNavigator.SelectSingleNode("url").Value;
return true;
}
}
[Serializable]
public class MyPicture : IMyObject
{
public int Id { get; set; }
public string Url { get; set; }
public bool State { get; set; }
private XPathNavigator _xmlNavigator;
public bool SetItemXml(XPathNavigator navigator)
{
_xmlNavigator = navigator.Clone();
Id = int.Parse(_xmlNavigator.SelectSingleNode("id").Value);
Url = _xmlNavigator.SelectSingleNode("url").Value;
return true;
}
}
public partial class _Default : System.Web.UI.Page
{
public IDictionary<string, IDictionary<int, IMyObject>> CreateObjects()
{
IDictionary<string, IDictionary<int, IMyObject>> objects = new Dictionary<string, IDictionary<int, IMyObject>>();
var reader = new XmlTextReader(new StringReader(@"<?xml version='1.0' encoding='utf-8'?><root><item><type>MyLink</type><id>1</id><url>http://www.google.com</url></item><item><type>MyLink</type><id>2</id><url>http://stackoverflow.com</url></item><item><type>MyPicture</type><id>3</id><url>http://static.adzerk.net/Advertisers/2565.png</url></item></root>"));
XPathNavigator navigator = new XPathDocument(reader).CreateNavigator();
XPathNodeIterator nodes = navigator.Select("//root/item");
while (nodes.MoveNext())
{
string classType = nodes.Current.SelectSingleNode("type").Value;
int id = int.Parse(nodes.Current.SelectSingleNode("id").Value);
if (!objects.ContainsKey(classType) || !objects[classType].ContainsKey(id))
{
IMyObject myObject = Activator.CreateInstance(Type.GetType(string.Concat("ObjectReference.", classType))) as IMyObject;
myObject.SetItemXml(nodes.Current);
if (!objects.ContainsKey(classType))
objects.Add(classType, new Dictionary<int, IMyObject>() { { id, myObject } });
else if (!objects[classType].ContainsKey(id))
objects[classType].Add(id, myObject);
}
}
return objects;
}
protected void Page_Load(object sender, EventArgs e)
{
IDictionary<string, IDictionary<int, IMyObject>> ObjectList = new Dictionary<string, IDictionary<int, IMyObject>>();
if (HttpContext.Current.Cache["ObjectCache"] != null)
{
ObjectList = (Dictionary<string, IDictionary<int, IMyObject>>)HttpContext.Current.Cache["ObjectCache"];
}
else
{
ObjectList = CreateObjects();
HttpContext.Current.Cache.Insert(
"ObjectCache",
ObjectList,
null,
DateTime.Now.AddMinutes(2),
System.Web.Caching.Cache.NoSlidingExpiration);
}
foreach(var parent in ObjectList)
{
foreach(var child in ObjectList[parent.Key])
{
if(false == child.Value.State)
{
//TODO... Note here
child.Value.State = true;
}
}
}
}
}
}
Have a look at something like XML Serializable Generic Dictionary
这篇关于如何序列化/反序列化一个字典变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!