System. Runtime. Serialization. SerializationException "type of unprocessed exception occurs in System. Runtime. Serialization. dll, runtimeexception
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 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 identifier, 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
"SystemStackOverflowException" type unprocessed exceptions occur in NewGzglDLL
Obviously, it's an endless loop... The set method is called infinitely.
Why is an endless loop:
If you assign a value to ParentId, Set {ParentId = value} will be called}
Then the ParentId in the set will call Set {ParentId = value} again. Is this an endless loop?
Correct practice:
Written as follows: Public int ParentId {get; set ;}
Or: private int _ parentId;
Public int ParentId
{
Get {return _ parentId ;}
Set {_ parentId = value ;}
}
The other attributes are also incorrect and must be changed. The get method is also.
SystemIOIOException "type unprocessed exception occurs in Systemdll
Isn't that clear?
Other information: Port "COM1" does not exist ." Error
So you should check whether port com1 is occupied by other programs. You can change the port