异常信息:
“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+LhFWFuaiU9paBTFOq4q4tw33+kf9Y518uj0m08z8ftfK827lhbduM2Y/tn3r+ofLQvJT92HO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M3ZTsvXnbtdVXtTKkUpaKNnavry+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 ...
随机推荐
-
js前台加密,java后台解密实现
参考资料: JS前台加密,java后台解密实现
-
easy datagrid 按钮控制
onBeforeLoad : function() {// 这里是紧接着你的修改按钮的 // 注意ID为你初始化工具栏按钮对应的ID var adminid=<%=Admin_Id%>+' ...
-
hihocoder #1224 : 赛车 dfs
#1224 : 赛车 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/problemset/problem/1224 ...
-
HTTP 和 Socket 的区别
要弄明白 http 和 socket 首先要熟悉网络七层:物 数 网 传 会 表 应,如图1 如图1 HTTP 协议:超文本传输协议,对应于应用层,用于如何封装数据. TCP/UDP 协议:传输控制协 ...
-
Dom4J对XML的创建、修改、删除等操作
Dom4j也可以很方便完成XML文档的创建.元素的修改.文档的查询遍历等,但dom4j稍比jdom复杂一点,不过在大片文档的情况下dom4j的性能要不jdom好. # 准备 首先,提供相关的jar包 ...
-
批处理bat脚本编写(附详细例子)
批处理bat脚本编写(附详细例子) 由于在项目开发的过程中经常需要编写bat脚本,而看大牛们编写 ...
-
rsync+inotify实现server实时备份
inotify实现对文件夹下文件进行监听的原理: inotify集成到内核中,通过内核提供的接口.使用inotify作为第三方的软件对文件夹变化进行监控. inotifywait命令能够对文件夹中的文 ...
-
CentOS、Ubuntu、Debian三个linux比较异同[转]
Linux有非常多的发行版本,从性质上划分,大体分为由商业公司维护的商业版本与由开源社区维护的免费发行版本. 商业版本以Redhat为代表,开源社区版本则以debian为代表.这些版本各有不同的特点, ...
-
php-fpm重启
Ubuntu 18.04服务器 修改php init 文件后(/etc/php/7.2/fpm/php.ini)需要重启php-fpm,方法是: kill -USR2 `cat /run/php/ph ...
-
apache禁止IP访问网站
参考资料: http://www.cnblogs.com/zhuangge/archive/2011/04/13/2014892.html 先引用一下上面资料的内容: 用apache搭建的WEB服务器 ...