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
- public class Test
- {
- private string _Field = "";
- public string Field
- {
- get { return _Field; }
- set { _Field = value; }
- }
- }
- public class TestInherited : Test
- {
- }
- public class Container
- {
- private Test ivField;
- public Test Field
- {
- get { return _Field; }
- set { _Field = value; }
- }
- }
- Container _Test = new Container();
- _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
Reprint: Serialization的更多相关文章
- Dubbo源码分析:Serialization
背景 顺序化逻缉处理! 类图 获取Serialization对象时序图 序列化
- [.net 面向对象程序设计进阶] (12) 序列化(Serialization)(四) 快速掌握JSON的序列化和反序列化
[.net 面向对象程序设计进阶] (12) 序列化(Serialization)(四) 快速掌握JSON的序列化和反序列化 本节导读: 介绍JSON的结构,在JS中的使用.重点说明JSON如何在.N ...
- C++的性能C#的产能?! - .Net Native 系列《二》:.NET Native开发流程详解
之前一文<c++的性能, c#的产能?!鱼和熊掌可以兼得,.NET NATIVE初窥> 获得很多朋友支持和鼓励,也更让我坚定做这项技术的推广者,希望能让更多的朋友了解这项技术,于是先从官方 ...
- boost--序列化库serialization
序列化可以把对象转化成一个字节流存储或者传输,在需要时再回复成与原始状态一致的等价对象.C++标准没有定义这个功能.boost.serialization以库的形式提供了这个功能,非常强大,可以序列化 ...
- Serialization之SOAP序列化
前言 XML序列化还可用于对象序列化符合SOAP规范的XML流.SOAP是一种简单的基于XML的协议,它使应用程序通过HTTP来交换信息.专门为使用XML来传输过程调用而设计的.如同常规的XML序列化 ...
- 跟我一起学WCF(7)——WCF数据契约与序列化详解
一.引言 在前面博文介绍到,WCF的契约包括操作契约.数据契约.消息契约和错误契约,前面一篇博文已经结束了操作契约的介绍,接下来自然就是介绍数据契约了.所以本文要分享的内容就是数据契约. 二.数据契约 ...
- LeetCode——Serialize and Deserialize Binary Tree
Description: Serialization is the process of converting a data structure or object into a sequence o ...
- 297. Serialize and Deserialize Binary Tree
题目: Serialization is the process of converting a data structure or object into a sequence of bits so ...
- Java 远程通讯技术及原理分析
在分布式服务框架中,一个最基础的问题就是远程服务是怎么通讯的,在Java领域中有很多可实现远程通讯的技术,例如:RMI.MINA.ESB.Burlap.Hessian.SOAP.EJB和JMS等,这些 ...
随机推荐
- c#后台输出javascript语句和一些通用验证的类
大家在用MVC的时候,经常会用到MODEL层的验证或者是正则表达式,我这边看到了一篇不错的文章,转载过来http://blog.csdn.net/accpxcb/article/details/311 ...
- MD5算法-爬虫学习(五)
在实现爬虫的时候,我们使用Hash结构去存储我们用过的URL的时候,有些URL可能长度很长,为了更加节省空间,我们就要对URL进行压缩,帮它减减肥,这个我们介绍这个MD5算法,可以对URL进行有效的压 ...
- malloc内存分配与free内存释放的原理
malloc内存分配与free内存释放的原理 前段时间一直想看malloc的原理,在搜了好几篇malloc源码后遂放弃,晦涩难懂. 后来室友买了本深入理解计算机系统的书,原来上面有讲malloc的原理 ...
- 应用facebook .net sdk
1.本博客主要介绍如何应用facebook .net SDK,实现发帖.点赞.上传照片视频等功能,更多关于facebook API,请参考:https://developers.facebook.co ...
- jquery 分页控件2
jquery 分页控件(二) 上一章主要是关于分页控件的原理,代码也没有重构.在这一章会附上小插件的下载链接,插件主要就是重构逻辑部分,具体可以下载源文件看下,源代码也有注释.为了测试这个插件是能用的 ...
- Debian 7.1设置中文环境
之前在Chinaunix上看过一个人写过博客,是关于介绍Debian设置中文环境的,在这里我针对Debian 7来补全一下. Debian是非常经典而优秀的Linux发行版了,Debian是个很大的家 ...
- HTML文档类型
在HMTL5中页面的最顶端代码就是: <!DOCTYPE html> 为何要如此定义.书写呢? 首先引入一个概念:文档类型,英译为:Document type,缩写成:doctype. 文 ...
- Please Send Me a Card
Please Send Me a Card 发现身边很多程序员都能看懂英文技术文章的60%-80%内容,但大家都有一个毛病,就是不会说,不会写作,在逛英文技术社区的时候,想发表点什么评论,总担心自己写 ...
- Event处理
Event处理 今天抽时间写了一部分Event处理方面的函数愈发的觉得jQuery的优秀,自己前期的想法太粗糙,造成后面这些函数参数很多,操作很很不直观,看样子是要重构的节奏,还好小伙儿伴们安慰,架构 ...
- Remobjects使用经验
RemObjects RemObjects提示:我们相信本文是正确的,但我们不做任何保证.在此感谢Henrick 写的文章,很高兴在此发表. 介绍RemObjects是功能强大可扩展的远程框架;但是当 ...