我不知道为什么我的类不能序列化

本文关键字:不能 序列化 我的 为什么 我不知道 | 更新日期: 2023-09-27 18:15:50

我完成了应用程序的编码。但是当我点击开始按钮时,我的应用程序引发了一个异常。:"(

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"类型"。"主题"不能连载。考虑用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]标记事件字段,否则这些事件的所有侦听器也将被序列化。

正如错误所述,您需要通过在类上使用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]到你的委托事件