我怎么能与序列化字典℃的对象,字符串,对象>属性? [英] How can I serialize an object with a Dictionary<string,object> property?

查看:108
本文介绍了我怎么能与序列化字典℃的对象,字符串,对象>属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例代码中,我得到这个错误




元素
TestSerializeDictionary123。 Customer.CustomProperties
VOM典型值
System.Collections.Generic.Dictionary`2 [System.String,
mscorlib程序,版本= 2.0.0.0,
区域性=中性,
公钥= b77a5c561934e089],[System.Object的,
mscorlib程序,版本= 2.0.0.0,
区域性=中性,
公钥= b77a5c561934e089]]可以
不会被序列化,因为它
实现的IDictionary。




当我拿出字典属性,它的工作原理精细



如何与序列化字典属性本客户对象吗?或者,我可以用什么替代类型字典,这将是序列化



 使用系统?; 
使用System.Collections.Generic;
使用的System.Xml.Serialization;
:使用System.IO;
使用的System.Xml;
使用System.Text;

命名空间TestSerializeDictionary123
{
公共类节目
{
静态无效的主要(字串[] args)
{
名单,LT ;客户>客户= Customer.GetCustomers();

Console.WriteLine(---序列化------------------);

的foreach(VAR的客户在客户)
{
Console.WriteLine(序列化+ customer.GetFullName()+...);
串XML = XmlHelpers.SerializeObject<客户>(客户);
Console.WriteLine(XML);
Console.WriteLine(反序列化...);
客户的customer2 = XmlHelpers.DeserializeObject<客户>(XML);
Console.WriteLine(customer2.GetFullName());
Console.WriteLine(---);
}

到Console.ReadLine();
}
}

公共静态类StringHelpers
{
公共静态字符串UTF8ByteArrayToString(字节[]字符)
{
UTF8Encoding编码=新UTF8Encoding();
字符串constructedString = encoding.GetString(字符);
回报(constructedString);
}

公共静态字节] StringToUTF8ByteArray(字符串pXmlString)
{
UTF8Encoding编码=新UTF8Encoding();
字节[]的字节数组= encoding.GetBytes(pXmlString);
返回的字节数组;
}
}

公共静态类XmlHelpers
{
公共静态字符串SerializeObject< T>(对象o)
{
的MemoryStream毫秒​​=新的MemoryStream();
XmlSerializer的XS =新的XmlSerializer(typeof运算(T));
XmlTextWriter的XTW =新的XmlTextWriter(MS,Encoding.UTF8);
xs.Serialize(XTW,O);
MS =(MemoryStream的)xtw.BaseStream;
返回StringHelpers.UTF8ByteArrayToString(ms.ToArray());
}

公共静态牛逼DeserializeObject< T>(串XML)
{
XmlSerializer的XS =新的XmlSerializer(typeof运算(T));
的MemoryStream毫秒​​=新的MemoryStream(StringHelpers.StringToUTF8ByteArray(XML));
XmlTextWriter的XTW =新的XmlTextWriter(MS,Encoding.UTF8);
回报率(T)xs.Deserialize(MS);
}
}

公共类客户
{
公众诠释标识{搞定;组; }
公共字符串名字{获得;组; }
公共字符串名字{获得;组; }
公共字符串街{搞定;组; }
公共字符串位置{搞定;组; }
公共字符串邮编{搞定;组; }
公众解释<字符串对象> CustomProperties来获取{;组; }

私人INT internalValue = 23;

公共静态列表<客户> GetCustomers的()
{
名单,LT;客户>客户=新的List<客户>();
customers.Add(新客户{ID = 1,名字=吉姆,姓氏=奇兵,邮编=23434});
customers.Add(新客户{ID = 2,名字=乔,姓氏=亚当斯,邮编=12312});
customers.Add(新客户{ID = 3,名字=杰克,姓氏=强生,邮编=23111});
customers.Add(新客户{ID = 4,名字=安吉,姓氏=Reckar,邮编=54343});
customers.Add(新客户{ID = 5,名字=亨利,姓氏=安徒生,邮编=16623});
回报客户;
}

公共字符串GetFullName()
{
返回姓++姓氏+(+ internalValue +);
}

}
}


解决方案

在我们的应用中,我们结束了使用:

 的DataContractSerializer XS =新的DataContractSerializer(typeof运算(T) ); 



而不是:

  XmlSerializer的XS =新的XmlSerializer(typeof运算(T)); 



这解决了这个问题,因为DataContractSerializer的支持词典。



另一个解决方案是部份 XML序列化通用词典的变通办法也适用于上面的例子,并没有从人们的链接使用它,可能是人这个问题时非常有用长时间的讨论。


In the example code below, I get this error:

Element TestSerializeDictionary123.Customer.CustomProperties vom Typ System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] can not be serialized because it implements IDictionary.

When I take out the Dictionary property, it works fine.

How can I serialize this Customer object with the dictionary property? Or what replacement type for Dictionary can I use that would be serializable?

using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Text;

namespace TestSerializeDictionary123
{
    public class Program
    {
        static void Main(string[] args)
        {
            List<Customer> customers = Customer.GetCustomers();

            Console.WriteLine("--- Serializing ------------------");

            foreach (var customer in customers)
            {
                Console.WriteLine("Serializing " + customer.GetFullName() + "...");
                string xml = XmlHelpers.SerializeObject<Customer>(customer);
                Console.WriteLine(xml);
                Console.WriteLine("Deserializing ...");
                Customer customer2 = XmlHelpers.DeserializeObject<Customer>(xml);
                Console.WriteLine(customer2.GetFullName());
                Console.WriteLine("---");
            }

            Console.ReadLine();
        }
    }

    public static class StringHelpers
    {
        public static String UTF8ByteArrayToString(Byte[] characters)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            String constructedString = encoding.GetString(characters);
            return (constructedString);
        }

        public static Byte[] StringToUTF8ByteArray(String pXmlString)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            Byte[] byteArray = encoding.GetBytes(pXmlString);
            return byteArray;
        }
    }

    public static class XmlHelpers
    {
        public static string SerializeObject<T>(object o)
        {
            MemoryStream ms = new MemoryStream();
            XmlSerializer xs = new XmlSerializer(typeof(T));
            XmlTextWriter xtw = new XmlTextWriter(ms, Encoding.UTF8);
            xs.Serialize(xtw, o);
            ms = (MemoryStream)xtw.BaseStream;
            return StringHelpers.UTF8ByteArrayToString(ms.ToArray());
        }

        public static T DeserializeObject<T>(string xml)
        {
            XmlSerializer xs = new XmlSerializer(typeof(T));
            MemoryStream ms = new MemoryStream(StringHelpers.StringToUTF8ByteArray(xml));
            XmlTextWriter xtw = new XmlTextWriter(ms, Encoding.UTF8);
            return (T)xs.Deserialize(ms);
        }
    }

    public class Customer
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Street { get; set; }
        public string Location { get; set; }
        public string ZipCode { get; set; }
        public Dictionary<string,object> CustomProperties { get; set; }

        private int internalValue = 23;

        public static List<Customer> GetCustomers()
        {
            List<Customer> customers = new List<Customer>();
            customers.Add(new Customer { Id = 1, FirstName = "Jim", LastName = "Jones", ZipCode = "23434" });
            customers.Add(new Customer { Id = 2, FirstName = "Joe", LastName = "Adams", ZipCode = "12312" });
            customers.Add(new Customer { Id = 3, FirstName = "Jack", LastName = "Johnson", ZipCode = "23111" });
            customers.Add(new Customer { Id = 4, FirstName = "Angie", LastName = "Reckar", ZipCode = "54343" });
            customers.Add(new Customer { Id = 5, FirstName = "Henry", LastName = "Anderson", ZipCode = "16623" });
            return customers;
        }

        public string GetFullName()
        {
            return FirstName + " " + LastName + "(" + internalValue + ")";
        }

    }
}

解决方案

In our application we ended up using:

DataContractSerializer xs = new DataContractSerializer(typeof (T));

instead of:

XmlSerializer xs = new XmlSerializer(typeof (T));

which solved the problem as DatacontractSerializer supports Dictionary.

Another solution is ths XML Serializable Generic Dictionary workaround also works in the above example, and there is a long discussion at that link from people using it, might be useful for people working with this issue.

这篇关于我怎么能与序列化字典℃的对象,字符串,对象&gt;属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆