字段是可序列化的类型的成员,但不可序列化的类型的成员 [英] Field is member of a type which is serializable but is of type which is not serializable
问题描述
我正在使用.NET Framework 4.7开发C#库.
I'm developing a C# library with .NET Framework 4.7.
我想将类 ProductionOrderXmlFile
转换为XML文件:
I want to convert the class ProductionOrderXmlFile
into a XML file:
[Serializable]
public class Level
{
[XmlElement("Id")]
public byte Id { get; set; }
[XmlElement("Name")]
public string Name { get; set; }
[XmlElement("CodeType")]
public byte CodeType { get; set; }
[XmlElement("CodeSourceType")]
public byte CodeSourceType { get; set; }
[XmlElement("HelperCodeType")]
public byte HelperCodeType { get; set; }
[XmlElement("HelperCodeSourceType")]
public byte HelperCodeSourceType { get; set; }
[XmlElement("PkgRatio")]
public int PkgRatio { get; set; }
}
[Serializable]
public class VarData
{
[XmlElement("VariableDataId")]
public string VariableDataId { get; set; }
[XmlElement("LevelId")]
public byte LevelId { get; set; }
[XmlElement("Value")]
public string Value { get; set; }
}
/// <summary>
/// Class to load a production order from a xml file.
/// </summary>
[Serializable, XmlRoot("root")]
public class ProductionOrderXmlFile
{
[XmlElement("ProductionOrderName")]
public string ProductionOrderName { get; set; }
[XmlElement("NumItems")]
public int NumItems { get; set; }
[XmlElement("ProductCode")]
public string ProductCode { get; set; }
[XmlElement("Reduction")]
public float Reduction { get; set; }
[XmlArray("Levels")]
[XmlArrayItem("Level")]
public List<Level> Levels { get; set; }
[XmlArray("VariableDatas")]
[XmlArrayItem("VariableData")]
public List<VarData> VariableData { get; set; }
}
但在字段 public List< Level>中,级别{获取;放;}
和 public List< VarData>VariableData {get;放;}
我得到警告:
But in fields public List<Level> Levels { get; set; }
and public List<VarData> VariableData { get; set; }
I get the warning:
警告CA2235字段级别是ProductionOrderXmlFile类型的成员,该成员可序列化,但类型为System.Collections.Generic.List,其不可序列化
并且:
警告CA2235字段VariableData是ProductionOrderXmlFile类型的成员,该成员可序列化,但属于System.Collections.Generic.List类型,不可序列化
为了避免这些警告,我该怎么办?
What do I need to do to avoid those warnings?
推荐答案
丢掉 [Serializable]
.扔掉它-全部. XmlSerializer
不在乎,您也不需要它.它无济于事,并引起此错误肯定的警告.
Lose the [Serializable]
. Just throw it away - all of them. XmlSerializer
doesn't care about it, and you don't need it. It isn't helping you, and is causing this false-positive warning.
[可序列化]
与本质上只是与 BinaryFormatter
相关,这通常不是一个好选择.
[Serializable]
relates essentially just to BinaryFormatter
, which usually isn't a good choice.
这篇关于字段是可序列化的类型的成员,但不可序列化的类型的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!