System.Runtime.Serialization.SerializationException”类型的未经处理的异常在 System.Runtime.Serialization.dll 中发生
异常信息:
“System.Runtime.Serialization.SerializationException”类型的未经处理的异常在 System.Runtime.Serialization.dll 中发生
其他信息: 不应为数据协定名称为“Teacher:http://schemas.datacontract.org/2004/07/ConsoleApplication3”的类型“ConsoleApplication3.Teacher”。请考虑使用 DataContractResolver,或将任何未知类型以静态方式添加到已知类型的列表。例如,可以使用 KnownTypeAttribute 特性,或者将未知类型添加到传递给 DataContractSerializer 的已知类型列表。
想要序列化的对象:
public class Student : People
{
public Int32 Age { get; set; }
}
public abstract class People
{
public string Name { get; set; }
public string Address { get; set; }
}
序列化的方法:
public static string WriteJson<T>(T t)
{
using (MemoryStream ms = new MemoryStream())
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
serializer.WriteObject(ms, t);
return Encoding.UTF8.GetString(ms.ToArray());
}
}
方法调用:
People p = new Student()
{
Name = "小明",
Address = "章丘路110号",
Age =
}; string jsonString = WriteJson<People>(p);
Console.WriteLine(jsonString);
然后就有了上面的异常
根据异常可以知道,虽然Student继承了People类,但是序列化的时候是People类型,也就是说序列化的时候并不知道 想要序列化的对象(Student类型),故产生了异常;根据异常提示,有两种解决方案:考虑使用 DataContractResolver,或将任何未知类型以静态方式添加到已知类型的列表。
经过了MSDN一番查询后,得到了两种可以序列化继承关系的对象的方法,
方式一,继承DataContractResolver类来实现:
public abstract class Animal
{
public string Name { get; set; }
} public class Bird : Animal
{
public string Color { get; set; }
public Feather BDeather { get; set; }
} public class Feather
{
public string Color { get; set; }
}
public static string WriteJson<T>(T t)
{
using (MemoryStream ms = new MemoryStream())
{
Assembly assembly = Assembly.Load(new AssemblyName("ConsoleApplication3"));
DataContractSerializer serializer = new DataContractSerializer(typeof(T), null, Int32.MaxValue, false, true, null, new MyDataContractResolver(assembly));
serializer.WriteObject(ms, t);
return Encoding.UTF8.GetString(ms.ToArray());
}
}
Animal animal = new Bird()
{
Name = "鸭子",
Color = "白色",
BDeather = new Feather() { Color="纯白色"}
};
string jsonString = WriteJson<Animal>(animal);
Console.WriteLine(jsonString);
public class MyDataContractResolver : DataContractResolver
{ private XmlDictionary dictionary = new XmlDictionary();
Assembly assembly;
public MyDataContractResolver(Assembly assembly)
{
this.assembly = assembly;
}
public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
{
Type type = knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, knownTypeResolver);
if (type == null)
{
type = Type.GetType(typeName + ", " + typeNamespace);
}
return type;
} public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
{
knownTypeResolver.TryResolveType(type, declaredType, knownTypeResolver, out typeName, out typeNamespace);
if (typeName == null || typeNamespace == null)
{
XmlDictionary dictionary = new XmlDictionary();
typeName = dictionary.Add(type.FullName);
typeNamespace = dictionary.Add(type.Assembly.FullName);
}
return true;
}
}
输出结果:
方式二,通过KnownTypeAttribute添加识别类型:
[KnownType(typeof(Student))]
[KnownType(typeof(Teacher))]
[KnownType(typeof(Pupil))]
[DataContract]
public abstract class People
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Address { get; set; }
}
public class Student : People
{ public Int32 Age { get; set; }
} public class Pupil : Student
{ }
public class Teacher : People
{
public Course T_course { get; set; }
} public class Course
{
public string Name { get; set; }
public int Times { get; set; }
}
输出结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoIAAACDCAIAAACWUe3OAAAS+0lEQVR4nO2dUXbbug5FNf9RaQJvLp1B30fbXIUADgGKFO14748umgKBA5ARrDZ2jwMA4GM4z/OZ5TcDwXRecEd+//7969ev3SoAAJ5l7HZ8Xojmr5eeuemfMQ9Efy8GarK6qrRhAPgIRK+K7q35+2/Uep/piJFz2rBF18Td7tO85ZoriTYMAB+EvYcmm2verXv7pg2/CN02bMdPt2H7xq15U/Cy+1rVGdnPfff6wBthN+LhFWFuaiU9paBTFOq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b3pKJGHO/WZNY70D/gZC52s5x09K/Lt1tPl/EfScoqebvqvkO8wkXM3ZTsvXnbtdVXtTKkUpaKNnavry+5eN2arz4Nr053UzqMj0YzdfdfYvPqfG56Idu5uuV3iTk5X6O6BOFVap7DvZuEGFWrFpB2LlDPzUV7VuAPzbtyuniPexyM4nyX/1qBbT0HePhPXNWvmhcjoUnV+BTa0sIzI+BSBkgaRDD3fpVTkvPPGzJV6mPOj7UtS79ANpA30kbhut7v12nNTh8Fv0bLHRZN0qGdek3fR+WpEdaOef6AOSaJTlDd256sz56UPnZfe0w09hWoPWKfkdeimqQ1sP2peNrucrKpb/81fZhm16lIL38i76Hw13n3f4UUQ5yTZnqN76JmjMdZOslmlicR/OMmyJDf36jN6KSYz8J3SAPCWZO6zmSXDt9SSgEXNkh7scn/7xvZubDtowwAAANugDQMAAGyDNgwAALAN2jAAAMA2/C+zbH5PbMqv+Qn/wt5Vwm8lAADAz6DfhqNf4K5SasPamE4MAAA/g04brj7CCrpP29HzN20YAAB+Ku13SttBpncmn56tf+tKO28MslkCAAC8JJ1f0YracHU8gGjnU/wDAABsJ9WGbUfU424H7dI8CvM0DAAAP5KFT8N5bK/VPunBAADwMxhsw4d58O3aC2jDAADwmYy3YdfmSPybbhLXzxTPAAAAL8Kt/2/YjgEAACDP+JdZznrqBQAA+Fj4TmkAAIBt0IYBAAC2QRsGAADYBm0YAABgG3/a8P8B4c3Mrbp7k04AAAAASUVORK5CYII=" alt="" />
关于更详细的描述,请参考以下MSDN的内容:
http://msdn.microsoft.com/zh-cn/library/system.runtime.serialization.knowntypeattribute.aspx
http://msdn.microsoft.com/zh-cn/library/system.runtime.serialization.datacontractserializer%28v=vs.110%29.aspx
System.Runtime.Serialization.SerializationException”类型的未经处理的异常在 System.Runtime.Serialization.dll 中发生的更多相关文章
- “System.InvalidOperationException”类型的未经处理的异常在 ESRI.ArcGIS.AxControls.dll 中发生
问题描述: 新手们进行ArcGIS ArcObject开发时经常会遇到各种十分古怪的问题,比如下面的这个问题: “System.InvalidOperationException”类型的未经处理的异常 ...
- “System.FormatException”类型的未经处理的异常在 System.IdentityModel.dll 中发生 其他信息: 十六进制字符串格式无效。
如果你的 WebService 客户端证书配置都没问题,唯独调用接口会出现这个错误 “System.FormatException”类型的未经处理的异常在 System.IdentityModel.d ...
- System.AccessViolationException”类型的未经处理的异常在 System.Data.dll 中发生。其它信息:尝试读取或写入受保护的内存。这通常指示其它内存已损坏。
错误背景: 操作系统:编程环境:VS2013. 语言:VB.net: 数据库:SQLserver2008 做数据库连接时.发生的错误: 错误提示为: 说明:用VB.net连接SQLServer数据 ...
- “System.Runtime.InteropServices.COMException”类型的第一次机会异常在 ESRI.ArcGIS.Version.dll 中发生
“System.Runtime.InteropServices.COMException”类型的第一次机会异常在 ESRI.ArcGIS.Version.dll 中发生 其他信息: The speci ...
- System.Windows.Forms.AxHost.InvalidActiveXStateException”类型的异常在 ESRI.ArcGIS.AxControls.dll 中发生,但未在用户代码中进行处理
private void CopyAndOverwriteMap() { //IObjectCopy接口变量申明 IObjectCopy objectCopy = new ObjectCopyClas ...
- “RazorEngine.Templating.TemplateCompilationException”类型的异常在 RazorEngine.NET4.0.dll 中发生,但未在用户代码中进行处理
错误信息: "RazorEngine.Templating.TemplateCompilationException"类型的异常在 RazorEngine.NET4.0.dll 中 ...
- “System.AccessViolationException”类型的未经处理的异常在 System.Data.dll 中发生 其他信息: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏
用管理员身份运行CMD:netsh winsock reset就可以解决
- “RazorEngine.Templating.TemplateParsingException”类型的异常在 RazorEngine.NET4.0.dll 中发生,但未在用户代码中进行处理 其他信息: Expected model identifier.
这个问题是由于在cshtml中 引用了model这个单词 它可能和Model在解析时有冲突. 解决方法:把model换成别的单词就可以了.
- MVC开发中的常见错误-04-“System.NullReferenceException”类型的异常在 BBFJ.OA.WebApp.dll 中发生,但未在用户代码中进行处理
未将对象引用设置到对象实例,又名空指针异常,伴随程序员开发的一生. 查看详细信息得知: SetUserRoleInfo() 首先想到的是 IBLL.IRoleInfoService RoleInfo ...
随机推荐
- 在Caffe上运行Cifar10示例
准备数据集 在终端上运行以下指令: cd caffe/data/cifar10 ./get_cifar10.sh cd caffe/examples/cifar10 ./create_cifar10. ...
- KafKa介绍(分布式架构)
介绍 Kafka是一个分布式的.可分区的.可复制的消息系统.它提供了普通消息系统的功能,但具有自己独特的设计.这个独特的设计是什么样的呢? 首先让我们看几个基本的消息系统术语: Kafka将消息以to ...
- Android开发中使用static变量应该注意的问题
package com.highxin.launcher01; import java.util.ArrayList; import java.util.HashMap; import java.ut ...
- SVN如何迁移到Git?
最近在使用华为软件开发云进行开发项目管理,目前华为软件开发云支持500M的免费项目空间,而且还可以在线编译和构建,不用担心开发环境和生产环境的不同,很好的体现了DevOps的开发理念. 之前一直是用s ...
- zabbix监控Elasticsearch集群
本节以 zabbix 为例,介绍如何使用监控系统完成 Elasticsearch 的监控报警. github 上有好几个版本的 ESZabbix 仓库,都源自 Elastic 公司员工 unterge ...
- What does a Bayes factor feel like?(转)
A Bayes factor (BF) is a statistical index that quantifies the evidence for a hypothesis, compared t ...
- 写给Android App开发人员看的Android底层知识(6)
(十一)BroadcastReceiver BroadcastReceiver,也就是广播,简称Receiver. 很多App开发人员表示,从来没用过Receiver.其实吧,对于音乐播放类App,用 ...
- cas4.2.7实现单点登录
准备前参考: cas server下载地址 cas client 下载地址 安全cookie setSecure详解 Spring通过构造方法注入的四种方式 cas 学习博文 自定义登录页和登录认证 ...
- redis3.2.6 集群安装
下载 [root@localhost ~]# cd /usr/local/src/ [root@localhost src]# wget http://download.redis.io/rele ...
- (数字IC)低功耗设计入门(六)——门级电路低功耗设计优化
三.门级电路低功耗设计优化 (1)门级电路的功耗优化综述 门级电路的功耗优化(Gate Level Power Optimization,简称GLPO)是从已经映射的门级网表开始,对设计进行功耗的优化 ...