c# – 我不知道为什么我的班级无法序列化

前端之家收集整理的这篇文章主要介绍了c# – 我不知道为什么我的班级无法序列化前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我完成了我的申请编码.但是,当我点击开始按钮时,我的应用程序引发了一个例外..:'(
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));
    }
}

以上是我的班级..

请帮我

解决方法

首先使用您要使用的序列化程序的相应属性来修饰类,例如: [Serializable]用于BinaryFormatter,或[DataContract]用于基于合同的格式化程序.

注意:如果使用[Serializable]属性,请记住使用[field:NonSerialized]标记事件字段,否则这些事件的所有侦听器也将被序列化.

猜你在找的C#相关文章