XmlSerializer won't serialize IEnumerable

后端 未结 7 945
滥情空心
滥情空心 2020-12-10 00:45

I have an invocation logger that is intended to record all method calls along with the parameters associated with the method using XmlSerializer. It works well for most of t

7条回答
  •  青春惊慌失措
    2020-12-10 01:08

    The way you serialize an IEnumerable property is with a surrogate property

    [XmlRoot]
    public class Entity {
       [XmlIgnore]
       public IEnumerable Foo { get; set; }
    
       [XmlElement, Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
       public List FooSurrogate { get { return Foo.ToList(); } set { Foo = value; } }
    }
    

    It's ugly, but it gets the job done. The nicer solution is to write a surrogate class (i.e. EntitySurrogate).

提交回复
热议问题