我正在尝试将XMLSerializer与城堡活动记录类一起使用,如下所示:
[ActiveRecord("Model")]
public class DataModel : ActiveRecordBase
{
private IList<Document> documents;
[XmlArray("Documents")]
public virtual IList<Document> Documents
{
get { return documents; }
set
{
documents = value;
}
}
}
但是,由于IList接口,XMLSerializer遇到了麻烦。 (引发异常:无法序列化'System.Collections.Generic.IList`1 .... 类型的成员'DataModel.Documents'。
我在别处读到这是XMLSerializer中的限制,建议的解决方法是将其声明为List<T>
接口。
因此,我尝试将IList<Document>
更改为List<Document>
。
这会导致ActiveRecord引发异常:
属性类型DataModel.Documents必须是接口(IList,ISet,IDictionary或其通用计数器部分)。您不能使用ArrayList或List作为属性类型。
所以,问题是:如何将XMLSerializer与包含IList成员的Castle ActiveRecord一起使用?
答案 0 :(得分:3)
有趣......我可以建议的最好的方法是在[XmlIgnore]
上使用Documents
- ActiveRecord是否有类似的方式忽略成员?你可以这样做:
[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); }
}
}
答案 1 :(得分:1)
Microsoft won't implement this,所以你必须解决它。一种方法是使用非通用IList
:
[ActiveRecord("Model")]
public class DataModel : ActiveRecordBase<DataModel> {
[XmlArray("Documents")]
[HasMany(typeof(Document)]
public virtual IList Documents {get;set;}
}
Here有关此错误的更多信息。