Exception information:
"System. runtime. serialization. serializationexception" type unprocessed exception occurs in system. runtime. serialization. dll
Other information: The data protocol name should not be "Teacher: http://schemas.datacontract.org/2004/07/leleapplication3” leapplication3.teacher ". Consider using datacontractresolver or adding any unknown type to a list of known types statically. For example, you can use the knowntypeattribute feature or add the unknown type to the list of known types passed to datacontractserializer.
Object To be serialized:
1 public class Student : People2 {3 public Int32 Age { get; set; }4 }
1 public abstract class People2 {3 public string Name { get; set; }4 public string Address { get; set; }5 }
Serialization method:
1 public static string WriteJson<T>(T t)2 {3 using (MemoryStream ms = new MemoryStream())4 {5 DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));6 serializer.WriteObject(ms, t);7 return Encoding.UTF8.GetString(ms.ToArray());8 }9 }
Method call:
1 people P = new student () 2 {3 name = "James", 4 address = "No. 110 Zhangqiu road", 5 age = 146 }; 7 8 string jsonstring = writejson <people> (p); 9 console. writeline (jsonstring );
Then the above exception occurs.
According to the exception, although student inherits the people class, it is serialized in The People type, that is, it does not know the object to be serialized (student type) during serialization, so an exception occurs; there are two solutions as prompted: Consider using datacontractresolver or adding any unknown type to a list of known types statically.
After some msdn queries, we get two methods for serializing objects with inheritance relationships,
Method 1: Inherit the datacontractresolver class for implementation:
1 public abstract class Animal 2 { 3 public string Name { get; set; } 4 } 5 6 public class Bird : Animal 7 { 8 public string Color { get; set; } 9 public Feather BDeather { get; set; }10 }11 12 public class Feather13 {14 public string Color { get; set; }15 }
1 public static string WriteJson<T>(T t) 2 { 3 using (MemoryStream ms = new MemoryStream()) 4 { 5 Assembly assembly = Assembly.Load(new AssemblyName("ConsoleApplication3")); 6 DataContractSerializer serializer = new DataContractSerializer(typeof(T), null, Int32.MaxValue, false, true, null, new MyDataContractResolver(assembly)); 7 serializer.WriteObject(ms, t); 8 return Encoding.UTF8.GetString(ms.ToArray()); 9 }10 }
1 animal = new bird () 2 {3 name = "Duck", 4 color = "white", 5 bdeather = new feather () {color = "white"} 6 }; 7 string jsonstring = writejson <animal> (animal); 8 console. writeline (jsonstring );
1 public class MyDataContractResolver : DataContractResolver 2 { 3 4 private XmlDictionary dictionary = new XmlDictionary(); 5 Assembly assembly; 6 public MyDataContractResolver(Assembly assembly) 7 { 8 this.assembly = assembly; 9 }10 public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)11 {12 Type type = knownTypeResolver.ResolveName(typeName, typeNamespace, declaredType, knownTypeResolver);13 if (type == null)14 {15 type = Type.GetType(typeName + ", " + typeNamespace);16 }17 return type;18 }19 20 public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)21 {22 knownTypeResolver.TryResolveType(type, declaredType, knownTypeResolver, out typeName, out typeNamespace);23 if (typeName == null || typeNamespace == null)24 {25 XmlDictionary dictionary = new XmlDictionary();26 typeName = dictionary.Add(type.FullName);27 typeNamespace = dictionary.Add(type.Assembly.FullName);28 }29 return true;30 }31 }
View code
Output result:
Method 2: add the recognition type through knowntypeattribute:
1 [KnownType(typeof(Student))] 2 [KnownType(typeof(Teacher))] 3 [KnownType(typeof(Pupil))] 4 [DataContract] 5 public abstract class People 6 { 7 [DataMember] 8 public string Name { get; set; } 9 [DataMember]10 public string Address { get; set; }11 }
1 public class Student : People 2 { 3 4 public Int32 Age { get; set; } 5 } 6 7 public class Pupil : Student 8 { 9 10 }
1 public class Teacher : People 2 { 3 public Course T_course { get; set; } 4 } 5 6 public class Course 7 { 8 public string Name { get; set; } 9 public int Times { get; set; }10 }
Output result:
For more details, refer to the following msdn content:
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 "type unprocessed exception occurs in system. runtime. serialization. dll