如何把一个字典:
如:System.Collections.Generic.Dictionary<Rect, string> sticks 先转换为字符串类型,作为一个方法的参数传递进入, 然后在方法里再把传入的 string 类型反转回原来的
System.Collections.Generic.Dictionary<Rect, string> sticks 类型谢谢
如:System.Collections.Generic.Dictionary<Rect, string> sticks 先转换为字符串类型,作为一个方法的参数传递进入, 然后在方法里再把传入的 string 类型反转回原来的
System.Collections.Generic.Dictionary<Rect, string> sticks 类型谢谢
你看下这个名字空间下的内容
System.Runtime.Serialization看能自己看明白或和你的需求不了,如果合你的要求在说怎么用吧
static void Main()
{
Dictionary<int, string> dic = new Dictionary<int, string>()
{
{1,"dddd"},
{43,"dddsd"},
{53,"d"}
}; string code = Convert.ToBase64String(Obj2Arr(dic)); Console.WriteLine("将Object转换成String");
Console.WriteLine(code);
Console.WriteLine(); Console.WriteLine("将String转换成Object");
Dictionary<int, string> convertedDic = (Dictionary<int, string>)Arr2Obj(Convert.FromBase64String(code));
foreach (KeyValuePair<int, string> pair in convertedDic)
Console.WriteLine(pair.Key + ":" + pair.Value); }
static byte[] Obj2Arr(Object obj)
{
if (obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
static Object Arr2Obj(byte[] bytes)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
ms.Write(bytes, 0, bytes.Length);
ms.Seek(0, SeekOrigin.Begin);
Object obj = (Object)bf.Deserialize(ms);
return obj;
}
http://stackoverflow.com/questions/1124597/why-isnt-there-an-xml-serializable-dictionary-in-net
http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx
的确是序列化问题。
顶 13楼