如何将 XMLSerializer 与包含 IList的 Castle ActiveRecord 一起使用 成员

发布于 2024-07-17 20:13:33 字数 906 浏览 8 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望笑 2024-07-24 20:13:33

有趣...我能建议的最好的建议是在 Documents 上使用 [XmlIgnore] - 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); }
    }
}

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); }
    }
}
金兰素衣 2024-07-24 20:13:33

Microsoft 不会实现此功能,因此您必须努力周围。 一种方法是使用非通用 IList

[ActiveRecord("Model")]
public class DataModel : ActiveRecordBase<DataModel> {
    [XmlArray("Documents")]
    [HasMany(typeof(Document)]
    public virtual IList Documents {get;set;}
}

这里是有关此错误的更多信息。

Microsoft won't implement this, so you have to work around it. One way would be to use the non-generic IList:

[ActiveRecord("Model")]
public class DataModel : ActiveRecordBase<DataModel> {
    [XmlArray("Documents")]
    [HasMany(typeof(Document)]
    public virtual IList Documents {get;set;}
}

Here's some more information about this bug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文