c# - 字段是可序列化类型的成员,但属于不可序列化类型

标签 c# xml serializable

我正在使用 .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<Level> Levels { get; set; }public List<VarData> VariableData { get; set; }我收到警告:

警告 CA2235 Field Levels 是可序列化的 ProductionOrderXmlFile 类型的成员,但属于不可序列化的 System.Collections.Generic.List 类型的成员

和:

警告 CA2235 Field VariableData 是可序列化的 ProductionOrderXmlFile 类型的成员,但属于不可序列化的 System.Collections.Generic.List 类型的成员

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

最佳答案

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

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

关于c# - 字段是可序列化类型的成员,但属于不可序列化类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50833769/

相关文章:

c# - 如何让两个应用程序通过 LAN 找到并连接?

c# - 在另一个线程中使用线程时发生内存泄漏

c# - 使用 get vs 属性 vs 方法

xml - 使用 XPath 拆分节点值

python - 元素树.ParseError : reference to invalid character number

python - 在 Python 中解析大型 XML 文件

c#比较对象而不创建dto

c# - 将数组 Json 发布到 .net 核心 web api Controller

java - Android Intent 创建对象的新实例

java - Hazelcast 可序列化映射 ClassNotFound 异常