【问题标题】:Field is member of a type which is serializable but is of type which is not serializable字段是可序列化类型的成员,但属于不可序列化类型
【发布时间】:2018-11-22 19:57:10
【问题描述】:

我正在使用 .NET Framework 4.7 开发 C# 库。

我想将ProductionOrderXmlFile类转换成XML文件:

[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&lt;Level&gt; Levels { get; set; }public List&lt;VarData&gt; VariableData { get; set; } 字段中我收到警告:

警告 CA2235 字段级别是 ProductionOrderXmlFile 类型的成员,该类型可序列化,但属于不可序列化的 System.Collections.Generic.List 类型

还有:

警告 CA2235 字段变量数据是 ProductionOrderXmlFile 类型的成员,该类型可序列化,但属于不可序列化的 System.Collections.Generic.List 类型

我需要做些什么来避免这些警告?

【问题讨论】:

    标签: c# xml serializable


    【解决方案1】:

    丢失[Serializable]。把它扔掉——所有的。 XmlSerializer 不关心它,你也不需要它。它对您没有帮助,并且会导致此误报警告。

    [Serializable]基本上只与BinaryFormatter相关,这通常不是一个好的选择。

    【讨论】:

    • 嘿。 SOAPFormattter 发生了什么?
    • @bommelding 自 .NET 2.0 以来已正式过时......不,我不是指 .NET Core 2.0,我真的是指 .NET 2.0
    猜你喜欢
    • 2016-05-22
    • 2015-07-20
    • 2014-06-07
    • 2021-11-27
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 2015-09-09
    相关资源
    最近更新 更多