我不知道为什么我的类无法序列化
我完成了应用程序的编码。但是当我单击开始按钮时,我的应用程序引发了异常..:'(
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,我可以知道原因。 “‘类型‘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));
}
}
上面是我的类..
请帮助我
I completed coding my application. But when I click start button, my app raised a exceptions.. :'(
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
So I saw argument e of 'Application_UnhandledException', and I could know the reason.
"'Type 'GPACalculator.Subject' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute."
I just make my class using default data types..
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));
}
}
The above is my class..
please help me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先使用您想要使用的序列化器的适当属性来装饰您的类,例如用于 BinaryFormatter 的
[Serialized]
或用于基于契约的格式化程序的[DataContract]
。注意:如果您使用 [Serialized] 属性,请记住使用
[field:NonSerialized]
标记事件字段,否则这些事件的所有侦听器也将被序列化。Start by decorating your classes with the appropriate attributes for the serializer you want to use, e.g.
[Serializable]
for BinaryFormatter, or[DataContract]
for contract based formatters.Note: If you use the [Serializable] attribute, remember to mark the event fields with
[field:NonSerialized]
otherwise all the listeners of those events will be serialized as well.由于错误指出,您需要通过使用类上的 DataContract 属性和其属性的 DataMember 属性来装饰类,将其定义为可序列化。只有用 DataMember 修饰的公共属性才能序列化。你的方法和事件不能。
更多信息
As the error states you need to define your class as serializable by decorating it with the DataContract attribute on the class and DataMember attribute to it's properties. Only public properties decorated with DataMember can be serialized. Your methods and events cannot.
More info here
您可以尝试使用属性标记该类
或将其标记为可序列化,如下所示:
You could try marking the class with the attribute
or mark it serializable like so:
在这种情况下,两个类都应该是可序列化的。将 [Serialized] 添加到类中
Both class should be serializable in this case. Add [Serializable] to the class
这个问题是多余的。
如何忽略二进制序列化的事件类成员?
您在类中错过了
[Serialized]
属性,并且由于事件不可序列化,您需要将其标记为委托事件的[field: NonSerialized]
。This question is redundant.
How to ignore Event class member for binary serialization?
You missed
[Serializable]
attribute in your class and also because event is not serialable you need to mark it as[field: NonSerialized]
to your delegate event.