如何将 XMLSerializer 与包含 IList的 Castle ActiveRecord 一起使用 成员
我正在尝试将 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
接口。
因此,我尝试将 IList
更改为 List
。 这会导致 ActiveRecord 引发异常: 属性 DataModel.Documents 的类型必须是接口(IList、ISet、IDictionary 或其通用对应部分)。 您不能使用 ArrayList 或 List 作为属性类型。
因此,问题是:如何将 XMLSerializer 与包含 IList 成员的 Castle ActiveRecord 一起使用?
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;
}
}
}
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....)
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.
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.
So, the question is: How do you use the XMLSerializer with a Castle ActiveRecord containing an IList member?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有趣...我能建议的最好的建议是在
Documents
上使用[XmlIgnore]
- ActiveRecord 是否有类似的忽略成员的方法? 你可以这样做:Interesting... the best I can suggest is to use
[XmlIgnore]
onDocuments
- and does ActiveRecord have a similar way of ignoring a member? You could do something like:Microsoft 不会实现此功能,因此您必须努力周围。 一种方法是使用非通用
IList
:这里是有关此错误的更多信息。
Microsoft won't implement this, so you have to work around it. One way would be to use the non-generic
IList
:Here's some more information about this bug.