2012-03-19 7 views
4

私はDictionaryを実装するジェネリッククラスを持っています。 KeyValuePairsの代わりに値をループするカスタムGetEnumeratorを作成しました。通常、キーについては気にしないからです。ここでは簡単サンプルです:DataContractに、基本クラスのGetEnumeratorを使用するように指示するにはどうすればよいですか?

public class AssetHolder<T> : Dictionary<string, T>, IEnumerable, INotifyCollectionChanged, INotifyPropertyChanged where T : Asset 
{ 
    // methods that don't relate to this post 
    ... 

    // enumeration methods 
    IEnumerator System.Collections.IEnumerable.GetEnumerator() // this one is called by WPF objects like DataGrids 
    { 
     return base.Values.GetEnumerator(); 
    } 
    new public IEnumerator<T> GetEnumerator() // this enumerator is called by the foreach command in c# code 
    { 
     return base.Values.GetEnumerator(); 
    } 
} 

それは直列化可能にするために、私はせずに、クラスの先頭に[のDataContract]を追加して、私は、(私は唯一のメソッドを追加しました)私のクラスにデータを追加しませんでした任意の[DataMember]タグ

タイプ 'Enumerator [System.String、SignalEngineeringTestPlanner.Asset]'のオブジェクトを 'System'にキャストすることができませんでした。これは、シリアライズ/デシリアライズの基本クラスのデータを使用するだけです。 Collections.Generic.IEnumerator`1 [System.Collections.Generic.KeyValuePair`2

これは、DataContractSerializerが子の列挙子を呼び出していることを意味し、ペアを予期しているが、Assetオブジェクトを取得しているので混乱していると思います。 (1)DataContractSerializerに基本クラスの列挙子を使用するように指示する方法、または(2)特殊な列挙関数を作成し、DataContractSerializerにその1つのみを使用するよう指示する方法がありますか?

答えて

1

あなたが代わりにあなたの派生クラスのクラスに辞書としてのタイプをマークすることができます。欠点は、それをキャストしなければならないということです(または、正しいタイプの別の参照を使用する必要があります)。

0

私はあなたがAssetHolderクラスでINotifyCollectionChangedおよびINotifyPropertyChangedのインタフェースを実装することによって取得されたエラーをeleviateするために管理:

[DataContract] 
public class AssetHolder<T> : Dictionary<string, T>, IEnumerable, INotifyCollectionChanged, INotifyPropertyChanged where T : Asset 
{ 
    IEnumerator IEnumerable.GetEnumerator() // this one is called by WPF objects like DataGrids 
    { 
     return base.Values.GetEnumerator(); 
    } 
    new public IEnumerator<T> GetEnumerator() // this enumerator is called by the foreach command in c# code 
    { 
     return base.Values.GetEnumerator(); 
    } 

    event NotifyCollectionChangedEventHandler INotifyCollectionChanged.CollectionChanged 
    { 
     add { throw new NotImplementedException(); } 
     remove { throw new NotImplementedException(); } 
    } 

    event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged 
    { 
     add { throw new NotImplementedException(); } 
     remove { throw new NotImplementedException(); } 
    } 
} 
+0

申し訳ありませんが、私は明確でない場合。問題はコンパイルできませんでした(私はすでにメンバーを実装していました)。この問題は、DataContractSerializerがクラスをどのように見ているかともっと関係していました。 – hypehuman

関連する問題