我完成了对应用程序的编码。但是,当我点击开始按钮时,我的应用程序引发了一个例外..:'(
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,我可以知道原因。 “'Type'GPACalculator.Subject'无法序列化。请考虑使用DataContractAttribute属性对其进行标记,并使用DataMemberAttribute属性标记要序列化的所有成员。”
我只是使用默认数据类型来创建我的课程。
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));
}
}
以上是我的班级..
请帮帮我
答案 0 :(得分:11)
首先使用您要使用的序列化程序的相应属性来修饰类,例如: BinaryFormatter为[Serializable]
,基于合同的格式化程序为[DataContract]
。
注意:如果您使用[Serializable]属性,请记住使用[field:NonSerialized]
标记事件字段,否则这些事件的所有侦听器也将被序列化。
答案 1 :(得分:6)
由于错误声明您需要将类定义为可序列化,方法是使用类的DataContract属性和其属性的DataMember属性进行装饰。只能序列化使用DataMember修饰的公共属性。你的方法和事件不能。
[DataContract]
public class Subject
{
[DataMember]
public string Name{get;set;}
}
答案 2 :(得分:2)
您可以尝试使用属性
标记该类[DataContract]
public class Subject : ...
或将其标记为可序列化:
[Serializable]
public class Subject : ...
答案 3 :(得分:0)
在这种情况下,两个类都应该是可序列化的。将[Serializable]添加到类
答案 4 :(得分:0)
这个问题是多余的。
How to ignore Event class member for binary serialization?
您错过了班级中的[Serializable]
属性,并且因为活动不是可连续的,您需要将其标记为[field: NonSerialized]
到您的委托活动。