How to use XMLSerializer with ActiveRecord Lock containing member IList <T>
I am trying to use an XMLSerializer with an active lock record class that looks like this:
[ActiveRecord("Model")] public class DataModel : ActiveRecordBase { private IList<Document> documents; [XmlArray("Documents")] public virtual IList<Document> Documents { get { return documents; } set { documents = value; } } } However, XMLSerializer is having difficulty with the IList interface. (Throws exception: Cannot serialize a DataModel.Documents element of type 'System.Collections.Generic.IList`1 .... )
I read elsewhere that this is a limitation in XMLSerializer, and the recommended workaround is to declare it as a List<T> interface.
So I tried changing IList<Document> to List<Document> . This leads to the fact that ActiveRecord throws an exception: The type of the DataModel.Documents property must be an interface (IList, ISet, IDictionary or their common counting parts). You cannot use ArrayList or List as a property type.
So the question is: how do you use an XMLSerializer with an ActiveRecord lock containing an IList member?
Interesting ... the best I can offer is to use [XmlIgnore] on Documents - and does ActiveRecord have a similar way to ignore a member? You can do something like:
[XmlIgnore] public virtual IList<Document> Documents { get { return documents; } set { documents = value; } } [Tell ActiveRecord to ignore this one...] [XmlArray("Documents"), XmlArrayItem("Document")] [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] public Document[] DocumentsSerialization { get { if(Documents==null) return null; return Documents.ToArray(); // LINQ; or do the long way } set { if(value == null) { Documents = null;} else { Documents = new List<Document>(value); } } } Microsoft will not implement this , so you have to get around this. One way is to use a non-generic IList :
[ActiveRecord("Model")] public class DataModel : ActiveRecordBase<DataModel> { [XmlArray("Documents")] [HasMany(typeof(Document)] public virtual IList Documents {get;set;} } Here is some more information about this error.