序列化类时没有标记为可序列化的错误 [英] 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);
其中 SerializableStructure
,和类型<$的C $ C> NN , SubNNs
和投入都是可序列化。 (输入包含一些分数
,长方形
和泛型列表)。
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键入MainProject.Main',版本= 1.0.0.0,文化=中立,公钥=空'未标记为可序列化。
Type 'MainProject.Main' in Assembly 'MainProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
为什么我给这个错误?主要是我的形式,而这些变量都位于我的表单。
Why I'm given this error? Main is my form, and these variables are located in my form.
我 NN的成功序列化类型
与的MemoryStream
和VB.NET,但我不知道为什么我得到这个错误?
I have successfully serialized Type of NN
with MemoryStream
and VB.NET , But I don't know why I'm getting this error?
下面是我的结构定义:
SerializableStructure:
SerializableStructure:
[Serializable()]
public class SerializableStructure
{
public List<Inputs> inputs = new List<Inputs>();
public NeuralNetwork NN;
public NeuralNetwork[] SubNNs;
}
输入:
Inputs:
[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
是非常大的结构
推荐答案
这几乎alwas意味着你有一个事件(或其他委托(!)。 - 也许在你的对象模型回调)的地方,那就是试图序列化。添加[非序列化]对任何事件的支持领域。如果您使用的是现场般的事件(最有可能的一种),是这样的:
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网(仅有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).
这篇关于序列化类时没有标记为可序列化的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!