简体   繁体   English

序列化只读集合,而无需实现IXmlSerializable

[英]Serialize read-only collection without implementing IXmlSerializable

I have a class : 我有一堂课:

[Serializable]
public class Profile
{
    [XmlAttribute]
    private string[] permissions;
    public string[] Permissions
    {
        get { return permissions; }
        set { permissions = value; }
    }
}

I want to serialize it in XML with XmlSerializer and I also have to be compliant with FxCop. 我想使用XmlSerializer以XML序列化它,并且我还必须与FxCop兼容。 The problem is that FxCop only wants to expose read-only collection for properties, but of course a ReadOnlyCollection is not serializable. 问题是FxCop只希望公开属性的只读集合,但是ReadOnlyCollection当然不能序列化。 I do not want to implement IXmlSerializable because it's too painful. 我不想实现IXmlSerializable,因为它太痛苦了。 Is there any other solution ? 还有其他解决方案吗?

Options: 选项:

  • you could use a List-of-String rather than a string-array; 您可以使用字符串列表而不是字符串数组; this doesn't need a setter, as the collection can be manipulated directly 这不需要设置器,因为可以直接操作集合
  • you could accept that fxcop is always going to be a generalisation, and that sometimes the generalisation is not appropriate to your specific context - meaning: simply ignore fxcop in this case 您可以接受fxcop始终是一个概括,并且有时该概括不适用于您的特定上下文-意思是:在这种情况下,只需忽略fxcop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM