4

次のようなキャッスル アクティブ レコード クラスで XMLSerializer を使用しようとしています。

[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 インターフェイスが原因で問題が発生します。(例外が発生します:タイプ 'System.Collections.Generic.IList`1.... のメンバー 'DataModel.Documents' をシリアル化できません。 )

これは XMLSerializer の制限であり、推奨される回避策は、List<T>代わりにインターフェイスとして宣言することです。

そのため、 を に変更してみIList<Document>ましたList<Document>。これにより、ActiveRecord で Exception: Type of property DataModel.Documents must be an interface (IList、ISet、IDictionary、またはそれらの一般的なカウンター パーツ) が発生します。プロパティ タイプとして ArrayList または List を使用することはできません。

問題は、IList メンバーを含む Castle ActiveRecord で XMLSerializer をどのように使用するかということです。

4

2 に答える 2

3

興味深い...私が提案できる最善の方法は、 [XmlIgnore]onを使用することです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); }
    }
}
于 2009-04-15T19:50:42.677 に答える
1

Microsoft はこれを実装しないため、回避する必要があります。1 つの方法は、非ジェネリックを使用することIListです。

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

このバグに関する詳細情報は次のとおりです。

于 2009-04-15T22:35:24.450 に答える