I don't know why my class cannot be serialized

I have finished coding my application. But when I click the start button, my application throws an 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 

So, I saw the argument e "Application_UnhandledException", and I could know the reason. "The type" GPACalculator.Subject "cannot be serialized. Consider labeling it with the DataContractAttribute attribute and mark all its members that you want to serialize 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)); } } 

Above is my class ..

Please help me

+6
source share
5 answers

Start by decorating your classes with the appropriate attributes for the serializer that you want to use, for example. [Serializable] for BinaryFormatter or [DataContract] for contract based formatter.

Note. If you use the [Serializable] attribute, be sure to mark the [field:NonSerialized] event fields, otherwise all listeners for these events will also be serialized.

+11
source

As the error says, you need to define your class as serializable by decorating it with the DataContract attribute of the class attribute and DataMember for its properties. Only public properties decorated with DataMember can be serialized. Your methods and events cannot.

 [DataContract] public class Subject { [DataMember] public string Name{get;set;} } 

More here

+6
source

You can try marking a class with an attribute

 [DataContract] public class Subject : ... 

or mark it serializable as follows:

 [Serializable] public class Subject : ... 
+2
source

Both classes must be serializable in this case. Add [Serializable] to the class

0
source

This question is superfluous.

How to ignore a member of an event class for binary serialization?

You missed the [Serializable] attribute in your class, and also because the event is not sequential, you need to mark it as [field: NonSerialized] for your delegation.

0
source

Source: https://habr.com/ru/post/902532/


All Articles