Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

c# - Field is member of a type which is serializable but is of type which is not serializable

I'm developing a C# library with .NET Framework 4.7.

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; }
}

But in fields public List<Level> Levels { get; set; } and public List<VarData> VariableData { get; set; } I get the warning:

Warning CA2235 Field Levels is a member of type ProductionOrderXmlFile which is serializable but is of type System.Collections.Generic.List which is not serializable

And:

Warning CA2235 Field VariableData is a member of type ProductionOrderXmlFile which is serializable but is of type System.Collections.Generic.List which is not serializable

What do I need to do to avoid those warnings?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

[Serializable] relates essentially just to BinaryFormatter, which usually isn't a good choice.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...