字段是可序列化的类型的成员,但不可序列化的类型的成员 [英] Field is member of a type which is serializable but is of type which is not serializable

查看:56
本文介绍了字段是可序列化的类型的成员,但不可序列化的类型的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆