Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I don't know why my class cannot be serialized

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

like image 894
Jonghwan Hyeon Avatar asked Nov 28 '11 10:11

Jonghwan Hyeon


People also ask

What makes a class not serializable?

By default, classes are not serializable unless they are marked with SerializableAttribute. During the serialization process all the public and private fields of a class are serialized by default.

What elements can not be serialize?

Static data members and transient data members are not saved via Serialization process.So, if you don't want to save value of a non-static data member then make it transient. 4. Constructor of object is never called when an object is deserialized.

What happens if a field within a serializable class is not also serializable?

It'll throw a NotSerializableException when you try to Serialize it.

What isn't serializable?

In Java, a NotSerializableException exception is thrown when an instance of a class must implement the Serializable interface. The exception is thrown by either the serialization runtime, or by the instance of the class. The argument for the NotSerializableException is the name of the class.


1 Answers

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.

like image 82
Anders Forsgren Avatar answered Oct 11 '22 04:10

Anders Forsgren