Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 只读集合的Xml序列化_C# - Fatal编程技术网

C# 只读集合的Xml序列化

C# 只读集合的Xml序列化,c#,C#,我有一些容器类通过ReadOnlyCollection公开它们的集合。提供了自定义方法来添加和删除集合,这些方法还执行一些自定义逻辑 例如: public class Foo { List<Bar> _barList = new List<Bar>(); public ReadOnlyCollection<Bar> BarList { get { return _barList.AsReadOnly(); }

我有一些容器类通过ReadOnlyCollection公开它们的集合。提供了自定义方法来添加和删除集合,这些方法还执行一些自定义逻辑

例如:

public class Foo
{
    List<Bar> _barList = new List<Bar>();

    public ReadOnlyCollection<Bar> BarList
    {
        get { return _barList.AsReadOnly(); }
    }

    public void AddBar(Bar bar)
    {
        if (bar.Value > 10)
            _barList.Add(bar);
        else
            MessageBox.Show("Cannot add to Foo. The value of Bar is too high");
    }
    public void RemoveBar(Bar bar)
    {
        _barList.Remove(bar);
        // Foo.DoSomeOtherStuff();
    }

}

public class Bar
{
    public string Name { get; set; }
    public int Value { get; set; }
}
公共类Foo
{
列表_barList=新列表();
公共只读集合栏列表
{
获取{return _barList.AsReadOnly();}
}
公共无效添加栏(栏)
{
如果(条形值>10)
_添加(条);
其他的
MessageBox.Show(“无法添加到Foo,Bar的值太高”);
}
公共无效删除栏(栏)
{
_删除(条);
//Foo.DoSomeOtherStuff();
}
}
公共类酒吧
{
公共字符串名称{get;set;}
公共int值{get;set;}
}
这一切都很好,但当我使用Xml序列化程序序列化Foo时,会抛出一个异常

有谁能提供一个好的方法来解决这个问题吗


谢谢

您无法反序列化
只读集合
,因为它没有
添加
方法。 要修复此问题,请使用第二个属性进行序列化:

[XmlIgnore()]
public ReadOnlyCollection<Bar> BarList
{
    get { return _barList.AsReadOnly(); }
}

[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
//[Obsolete("This is only for serialization process", true)]
[XmlArray("BarList")]
[XmlArrayItem("Bar")]
public List<Bar> XmlBarList
{
    get { return _barList; }
    set { _barList = value; }
}
[XmlIgnore()]
公共只读集合栏列表
{
获取{return _barList.AsReadOnly();}
}
[可浏览(错误)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
//[过时(“这仅适用于序列化过程”,true)]
[XmlArray(“BarList”)]
[XmlArrayItem(“Bar”)]
公共列表XmlBarList
{
获取{return\u barList;}
设置{u barList=value;}
}

事实上,这是行不通的。所以不要那样做。此外,除了非常痛苦的
IXmlSerializable
之外,没有检测xml序列化的钩子

因此,要么:

  • 此处不要使用只读集合
  • 实现
    IXmlSerializable
    (棘手)
  • 有一个双API(一个只读,一个非;将“非”序列化为
    XmlSerializer
    仅处理publicx成员)
  • 使用单独的DTO进行序列化

XML序列化仅序列化具有getter和setter的属性。
你可以使用,或者。

@Frederik:只要测试一下,就知道你是对的。
过时的
属性将不会序列化。谢谢你的评论,很抱歉我坚持这么做。嗨,谢谢你的回复,我还发现添加过时的标签可以防止serialisation@Cadair:你觉得这些答案有用吗?你用了什么?@Jalal,我真的没找到我想要的地方。谢谢你的建议。问题是我不想公开一个公共列表,因为这样可以绕过我的自定义Add()和Remove()方法直接将对象添加到列表中。就xml序列化而言,这似乎是唯一的方法,我将不得不重新考虑它抛出InvalidOperationException,因为反射Foo时出错。