Serialize Objects using xmlSerializer.Serialize and IEnumerable objects

后端 未结 2 1046
名媛妹妹
名媛妹妹 2021-01-04 08:20

I have an object that holds an object that is definded as IEnumerable, i.e.

[Serializable]
[XmlRoot(\"MyObject\")]
public class MyObject
{
    [XmlAttribute]         


        
相关标签:
2条回答
  • 2021-01-04 08:50

    In general terms, you should use Collection<T> if you intend users of your class to be free to modify it, or ReadOnlyCollection<T> if you do not. It is not recommended to use List<T> in public interfaces (source).

    However, as this class is for XML Serialization and thus presumably isn't used for any purpose other than representing XML as an object model, the rules don't really apply in the same way as a 'normal' API because it will have no consumers other than the XmlSerializer, and so it doesn't really matter that much what you use. I'd probably still stick to the recommendations though.

    0 讨论(0)
  • 2021-01-04 08:57

    If you don't mind the list being editable i.e. Adding/Removing publically then List<> would be fine. Otherwise, you should use ReadOnlyCollection

    0 讨论(0)
提交回复
热议问题