程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> System.Runtime.Serialization.SerializationException”類型的未經處理的異常在 System.Runtime.Serialization.dll 中發生,runtimeexception

System.Runtime.Serialization.SerializationException”類型的未經處理的異常在 System.Runtime.Serialization.dll 中發生,runtimeexception

編輯:C#入門知識

System.Runtime.Serialization.SerializationException”類型的未經處理的異常在 System.Runtime.Serialization.dll 中發生,runtimeexception


異常信息:

“System.Runtime.Serialization.SerializationException”類型的未經處理的異常在 System.Runtime.Serialization.dll 中發生

其他信息: 不應為數據協定名稱為“Teacher:http://schemas.datacontract.org/2004/07/ConsoleApplication3”的類型“ConsoleApplication3.Teacher”。請考慮使用 DataContractResolver,或將任何未知類型以靜態方式添加到已知類型的列表。例如,可以使用 KnownTypeAttribute 特性,或者將未知類型添加到傳遞給 DataContractSerializer 的已知類型列表。

 

想要序列化的對象:

1     public class Student : People
2     {
3         public Int32 Age { get; set; }
4     }
1     public abstract class People
2     {
3         public string Name { get; set; }
4         public string Address { get; set; }
5     }

序列化的方法:

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         }

方法調用:

1             People p = new Student()
2             {
3                 Name = "小明",
4                 Address = "章丘路110號",
5                 Age = 14
6             };
7 
8             string jsonString = WriteJson<People>(p);
9             Console.WriteLine(jsonString);

然後就有了上面的異常

根據異常可以知道,雖然Student繼承了People類,但是序列化的時候是People類型,也就是說序列化的時候並不知道 想要序列化的對象(Student類型),故產生了異常;根據異常提示,有兩種解決方案:考慮使用 DataContractResolver,或將任何未知類型以靜態方式添加到已知類型的列表。

經過了MSDN一番查詢後,得到了兩種可以序列化繼承關系的對象的方法,

方式一,繼承DataContractResolver類來實現:

 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 Feather
13     {
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 = "鴨子",
4                 Color = "白色",
5                 BDeather = new Feather() { Color="純白色"}
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

輸出結果:

方式二,通過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     }

輸出結果:

關於更詳細的描述,請參考以下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


“SystemStackOverflowException”類型的未經處理的異常出現在 NewGzglDLL 中

很明顯是死循環了啊。。。set方法被無限調用了。

為什麼會死循環:
你給ParentId賦值,就會調用 Set{ ParentId = value}
然後set 裡面的ParentId又會調用Set{ ParentId = value} 這不死循環了麼?

正確的做法:
寫成下面:Public int ParentId { get ; set; }
或者: private int _parentId ;
Public int ParentId
{
get { return _parentId;}
set { _parentId = value;}

}

其他的屬性也都錯了,都要改過來。get方法也是。
 

SystemIOIOException”類型的未經處理的異常出現在 Systemdll 中

不是說清楚了麼

其他信息: 端口"COM1”不存在。”的錯誤

所以你應該去檢查com1端口是不是已經被別的程序占用了,你可以換個端口的
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved