Reprint: Serialization

Having just recently ran into some major serialization issues I’m going to list some of the errors and pitfalls that I ran into.

Some of the errors encountered

Error one
“<ClassName> is inaccessible due to its protection level. Only public types can be processed.”

It means that the Class you are trying to serialize is not marked as public and hence the serializor can not access it. Depending on the Class scope this may not be a problem e.g. the serialization code and the class are both in the same scope.

Error Two
“Cannot serialize member <Property Name> of type <Type> because it is an interface.”

It means that one of the members in your class is defined as an interface. An interface can never be serialized since the serializor will not know which instance of the interface to use.

Error Three
The type <Type> was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.

This error is caused when trying to serialize an inherited class

E.g. the following example will cause this problem

  1. public class Test
  2. {
  3. private string _Field = "";
  4. public string Field
  5. {
  6. get { return _Field; }
  7. set { _Field = value; }
  8. }
  9. }
  10. public class TestInherited : Test
  11. {
  12. }
  13. public class Container
  14. {
  15. private Test ivField;
  16. public Test Field
  17. {
  18. get { return _Field; }
  19. set { _Field = value; }
  20. }
  21. }
  22. Container _Test = new Container();
  23. _Test.Field = new TestInherited();

The issue is that the Container.Field is defined as Test but instantiated as TestInherited. Their are two solutions for this problem
1) Add the attribute [XmlInclude(typeof(TestInherited))] to the Test class
2) new XmlSerializer(typeof(Container), new Type[] { typeof(TestInherited) });

The second method is preferred. With the first method you have to decorate your base classes to which you may not always have access to, secondly the base class should not be aware of any class that inherits from it.

Note:特别的,当一个对象中的属性为基类或者object类型时,他的依赖注入的类型(即运行时类型)必须明确使用 XmlInclude属性标明序列化的类型,否则序列化会出错。

转载自 http://www.johnsoer.com/blog/?p=125

时间: 2024-10-28 22:51:22

Reprint: Serialization的相关文章

《攻城Online》快速原型:服务端设计

“攻城”服务端采用Photon引擎的框架,其主要逻辑如以下UML所示. 服务端的启动入口为ServerApplication,该类包含着相关的Collection数据集合,而Collection内又有与数据库文件夹Database关联的文件.两个文件夹的内容如图. 简单来说,ServerApplication内缓存着各类数据,并完成与数据库等的关联.而本篇的重点是ServerPeer这个类.下面介绍什么是Peer. 每当一个客户端连接到服务端时,服务端会自动生成一个客户端连接实例,称其为Peer

[.net 面向对象程序设计进阶] (12) 序列化(Serialization)(四) 快速掌握JSON的序列化和反序列化

[.net 面向对象程序设计进阶] (12) 序列化(Serialization)(四) 快速掌握JSON的序列化和反序列化 本节导读: 介绍JSON的结构,在JS中的使用.重点说明JSON如何在.NET中快带序列化和反序列化.最后介绍在使用.NET序列化JSON过程中的注意事项. 读前必备: A.泛型       [.net 面向对象编程基础]  (18) 泛型 B.LINQ使用  [.net 面向对象编程基础] (20) LINQ使用 1. 关于JSON JSON的全称是”JavaScrip

日志系统之扩展Flume-LineDeserializer

本人博客文章如未特别注明皆为原创!如有转载请注明出处:http://blog.csdn.net/yanghua_kobe/article/details/46595401 继续闲聊日志系统,在之前的博文里已提到我们在日志收集上的选择是flume-ng.应用程序将日志打到各自的日志文件或指定的文件夹(日志文件按天滚动),然后利用flume的agent去日志文件中收集. Deserializer简介 flume将一条日志抽象成一个event.这里我们从日志文件中收集日志采用的是定制版的SpoolDi

C# - .NET自带的两种JSON序列化

http://www.cnblogs.com/yubinfeng/p/4637165.html Serialization,Deserialize 程序集:System.Web.Extensions.dll 命名空间:System.Web.Script.Serialization 类:Serialization,Deserialize ///<summary>/// 类:武林高手 /// MartialArtsMaster ///</summary> [DataContract]

跟我一起学WCF(7)——WCF数据契约与序列化详解

一.引言 在前面博文介绍到,WCF的契约包括操作契约.数据契约.消息契约和错误契约,前面一篇博文已经结束了操作契约的介绍,接下来自然就是介绍数据契约了.所以本文要分享的内容就是数据契约. 二.数据契约的介绍 在WCF中,服务契约定义了可供调用的服务操作方法,而数据契约则是定义了服务端和客户端之间传送的自定义类型,在WCF项目中,必不可少地是传递数据,把客户端需要传递的数据传送到服务中,服务接收到数据再对其进行处理.然而在WCF中,传递的类型必须标记为DataContractAttribute属性

java ArrayList 源码浅析

学习的东西越多就会发现自己越无知,最近看各种大牛的博客之类,深觉自己的无知啊,瀑布汗...摆正心态,慢慢学习,希望勤能补拙了. ArrayList算是Java集合框架中相对简单的一个了,学习数据结构的时候很多人也会选择去自己实现一个类似功能的数组的线性存储,其实ArrayList也是如此,只是其开发人员写的更加正规一些,下面就看下源码去看下他们的思路. 1. 定义 public class ArrayList<E> extends AbstractList<E> implement

【树】Serialize and Deserialize Binary Tree

题目: Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another co

LeetCode——Serialize and Deserialize Binary Tree

Description: Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or a

Java 远程通讯技术及原理分析

在分布式服务框架中,一个最基础的问题就是远程服务是怎么通讯的,在Java领域中有很多可实现远程通讯的技术,例如:RMI.MINA.ESB.Burlap.Hessian.SOAP.EJB和JMS等,这些名词之间到底是些什么关系呢,它们背后到底是基于什么原理实现的呢,了解这些是实现分布式服务框架的基础知识,而如果在性能上有高的要求的话,那深入了解这些技术背后的机制就是必须的了. 1 基本原理 要实现网络机器间的通讯,首先得来看看计算机系统网络通信的基本原理,在底层层面去看,网络通信需要做的就是将流从