如何使用XMLSerializer的一个城堡的ActiveRecord包含一个IList< T>会员

如何使用XMLSerializer的一个城堡的ActiveRecord包含一个IList< T>会员

问题描述:

我想使用XmlSerializer的与它看起来像下面的一个城堡活动记录类:

I am trying to use the XMLSerializer with a castle active record class which looks like the following:

[ActiveRecord("Model")]
public class DataModel : ActiveRecordBase
{
    private IList<Document> documents;

    [XmlArray("Documents")]
    public virtual IList<Document> Documents
    {
        get { return documents; }
        set
        {
            documents = value;    
        }
    }
}

不过,XmlSerializer的运行,因为IList接口的麻烦。 (引发异常:无法序列类型的成员DataModel.Documents''System.Collections.Generic.IList`1 ....

However, the XMLSerializer runs into trouble because of the IList interface. (Raises exception: Cannot serialize member 'DataModel.Documents' of type 'System.Collections.Generic.IList`1....)

我在其他地方看,这是在XmlSerializer的限制和建议的解决方法是将其声明为名单,其中,T&GT; 接口,而不是

I read elsewhere that this is a limitation in the XMLSerializer and the recommended workaround is to declare it as a List<T> interface instead.

因此​​,我试图改变的IList&LT;文件&GT; 名单,其中,文件&GT; 。 这导致的ActiveRecord引发异常: 属性DataModel.Documents类型必须是接口(IList的,的ISet,IDictionary的还是他们的通用零件柜)。不能使用的ArrayList或List作为属性类型。

Therefore I tried changing the IList<Document> to List<Document>. This causes ActiveRecord to raise an Exception: Type of property DataModel.Documents must be an interface (IList, ISet, IDictionary or their generic counter parts). You cannot use ArrayList or List as the property type.

所以,问题是:你如何使用XmlSerializer的一个城堡的ActiveRecord包含一个IList成员

So, the question is: How do you use the XMLSerializer with a Castle ActiveRecord containing an IList member?

有趣的...最好的,我可以建议是使用 [XmlIgnore] 文件 - 和它的ActiveRecord有忽视成员的类似的方式?你可以这样做:

Interesting... the best I can suggest is to use [XmlIgnore] on Documents - and does ActiveRecord have a similar way of ignoring a member? You could 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); }
    }
}