序列化类时未标记为可序列化错误

Not marked as serializable error when serializing a class(序列化类时未标记为可序列化错误)

本文介绍了序列化类时未标记为可序列化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码使用 BinaryFormatter 序列化一个结构:

I am serializing an structure by using BinaryFormatter using this code:

private void SerializeObject(string filename, SerializableStructure objectToSerialize)
{
    Stream stream = File.Open(filename, FileMode.Create);
    BinaryFormatter bFormatter = new BinaryFormatter();
    bFormatter.Serialize(stream, objectToSerialize);
    stream.Close();
}

哪个 objectToSerialize 是我的结构,我这样调用这个函数:

Which objectToSerialize is my structure, I'm calling this function like this:

SerializableStructure s = new SerializableStructure();
s.NN = NN;
s.SubNNs = SubNNs;
s.inputs = inputs;
SerializeObject(Application.StartupPath + "\Save\" + txtSave.Text + ".bin", s);

SerializableStructureNN 的类型、SubNNs 和输入是可序列化的.(输入包含一些 PointsRectangles 和通用列表).

Which SerializableStructure, and Type of NN, SubNNs and inputs are serializable. (inputs contains some Points, Rectangles and generic lists).

现在,当我运行代码时,出现此错误:

Now, When I run my code, I am given this error:

程序集中的类型MainProject.Main"MainProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"未标记为可序列化.

Type 'MainProject.Main' in Assembly 'MainProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

为什么我会收到这个错误?Main 是我的表单,这些变量位于我的表单中.

Why I'm given this error? Main is my form, and these variables are located in my form.

我已经使用 MemoryStream 和 VB.NET 成功序列化了 NN 的类型,但我不知道为什么会出现此错误?

I have successfully serialized Type of NN with MemoryStream and VB.NET , But I don't know why I'm getting this error?

这是我的结构的定义:

序列化结构:

[Serializable()]
public class SerializableStructure
{
    public List<Inputs> inputs = new List<Inputs>();
    public NeuralNetwork NN;
    public NeuralNetwork[] SubNNs;
}

输入:

[Serializable()]
public class Inputs
{
    public string XPath { get; set; }
    public string YPath { get; set; }
    public string ImagePath { get; set; }
    public string CharName { get; set; }
    public string CharBaseName { get; set; }
    public List<double> x { get; set; }
    public List<double> y { get; set; }
    public List<double> DotsX { get; set; }
    public List<double> DotsY { get; set; }
    public List<Point> GravityCenters { get; set; }
    public List<Rectangle> Bounds { get; set; }

    public override string ToString()
    {
        return CharName;
    }

    public Inputs(string xPath, string yPath, string imagePath, string charName, string charBaseName)
    {
        XPath = xPath;
        YPath = yPath;
        ImagePath = imagePath;
        CharName = charName;
        CharBaseName = charBaseName;
        x = new List<double>();
        y = new List<double>();
        GravityCenters = new List<Point>();
        Bounds = new List<Rectangle>();
    }
}

还有 NN 是一个非常大的结构(!).

Also NN is very big structure(!).

推荐答案

这几乎总是意味着您的对象模型中的某处有一个事件(或其他委托 - 可能是回调),它正试图被序列化.将 [NonSerialized] 添加到任何事件支持字段.如果您使用的是类似字段的事件(最有可能的类型),这是:

This almost alwas means you have an event (or other delegate - maybe a callback) somewhere in your object model, that is trying to be serialized. Add [NonSerialized] to any event-backing fields. If you are using a field-like event (the most likely kind), this is:

[field:NonSerialized]
public event SomeDelegateType SomeEventName;

或者:大多数其他序列化程序不查看事件/委托,并提供更好的版本兼容性.切换到 XmlSerializer、JavaScriptSerializer、DataContractSerializer 或 protobuf-net(仅 4 个示例)也可以通过不尝试这样做的简单方法来解决此问题(您几乎从不打算将事件视为 DTO 的一部分).

Alternatively: most other serializers don't look at events/delegates, and provide better version-compatibility. Switching to XmlSerializer, JavaScriptSerializer, DataContractSerializer or protobuf-net (just 4 examples) would also solve this by the simple approach of not trying to do this (you almost never intend for events to be considered as part of a DTO).

这篇关于序列化类时未标记为可序列化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:序列化类时未标记为可序列化错误