c# – 我不知道为什么我的班级无法序列化
发布时间:2020-12-15 18:30:14 所属栏目:百科 来源:网络整理
导读:我完成了我的申请编码.但是,当我点击开始按钮时,我的应用程序引发了一个例外..:'( A first chance exception of type 'System.Runtime.Serialization.InvalidDataContractException' occurred in System.Runtime.Serialization.dllA first chance exception
我完成了我的申请编码.但是,当我点击开始按钮时,我的应用程序引发了一个例外..:'(
A first chance exception of type 'System.Runtime.Serialization.InvalidDataContractException' occurred in System.Runtime.Serialization.dll A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll A first chance exception of type 'System.Runtime.Serialization.InvalidDataContractException' occurred in System.Runtime.Serialization.dll 所以我看到’Application_UnhandledException’的参数e,我可以知道原因. 我只是使用默认数据类型来创建我的类. public class Subject : INotifyPropertyChanged { private string name; private GradePoint gradePoint; private int credit; public Subject(string name) { Name = name; GradePoint = new GradePoint(); } public string Name { get { return name; } set { Debug.WriteLine("Name: " + value); if (name != value) { name = value; OnPropertyChanged("Name"); } } } public GradePoint GradePoint { get { return gradePoint; } set { if (gradePoint != value) { gradePoint = value; OnPropertyChanged("GradePoint"); } } } public int Credit { get { return credit; } set { if (credit != value) { credit = value; OnPropertyChanged("Credit"); } } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this,new PropertyChangedEventArgs(propertyName)); } } public class GradePoint : INotifyPropertyChanged { private string mainGradePoint; private string subGradePoint; private int credit; public int Credit { get { return credit; } set { if (credit != value) { credit = value; OnPropertyChanged("Credit"); } } } public string MainGradePoint { get { return mainGradePoint; } set { value = value.ToUpper(); if (mainGradePoint != value) { mainGradePoint = value; OnPropertyChanged("MainGradePoint"); } } } public string SubGradePoint { get { return subGradePoint; } set { if (subGradePoint != value) { subGradePoint = value; OnPropertyChanged("SubGradePoint"); } } } public override string ToString() { return string.Format("{0}{1}",mainGradePoint,subGradePoint); } public double ToDouble(double perfectScore = 4.5F) { double gap = perfectScore - Math.Floor(perfectScore); double value; switch (mainGradePoint) { case "A": value = 4.0; break; case "B": value = 3.0; break; case "C": value = 2.0; break; case "D": value = 1.0; break; default: value = 0.0; return value; } switch (subGradePoint) { case "+": value += gap; break; case "-": value -= gap; break; } return value; } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this,new PropertyChangedEventArgs(propertyName)); } } 以上是我的班级.. 请帮我 解决方法
首先使用您要使用的序列化程序的相应属性来修饰类,例如: [Serializable]用于BinaryFormatter,或[DataContract]用于基于合同的格式化程序.
注意:如果使用[Serializable]属性,请记住使用[field:NonSerialized]标记事件字段,否则这些事件的所有侦听器也将被序列化. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |