[英]I don't know why my class cannot be serialized
我完成了我的申請編碼。 但是,當我點擊開始按鈕時,我的應用程序引發了一個例外..:'(
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));
}
}
以上是我的班級..
請幫我
首先使用您想要使用的序列化程序的相應屬性來修飾類,例如,用於BinaryFormatter的[Serializable]
,或用於基於合同的格式化程序的[DataContract]
。
注意:如果使用[Serializable]屬性,請記住使用[field:NonSerialized]
標記事件字段,否則這些事件的所有偵聽器也將被序列化。
由於錯誤聲明您需要將類定義為可序列化,方法是使用類的DataContract屬性和其屬性的DataMember屬性進行裝飾。 只能序列化使用DataMember修飾的公共屬性。 你的方法和事件不能。
[DataContract]
public class Subject
{
[DataMember]
public string Name{get;set;}
}
您可以嘗試使用該屬性標記該類
[DataContract]
public class Subject : ...
或標記它可序列化如下:
[Serializable]
public class Subject : ...
在這種情況下,兩個類都應該是可序列化的。 將[Serializable]添加到類中
這個問題是多余的。
您錯過了類中的[Serializable]
屬性,並且因為事件不是可連續的,您需要將其標記為[field: NonSerialized]
到您的委托事件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.