質問

私はSOとインターネット全体を見てきましたが、私が欠けているものと、そのような型のf#で汎用IListインターフェイスを実装する方法を理解できません:

type OrCondition() as self =
    inherit Condition()
    member val Conditions: List<Condition> = new List<Condition>() with get, set
    interface IList<Condition> with
        member this.Item 
            with get(index) = self.Conditions.[index]
            and  set(index)(value) = self.Conditions.[index] <- value
        member this.IndexOf item = self.Conditions.IndexOf(item)
        member this.Insert(index, item) = self.Conditions.Insert(index, item)
        member this.RemoveAt(index) = self.Conditions.RemoveAt(index)
        member this.Count with get() = self.Conditions.Count
        member this.IsReadOnly with get() = false
        member this.Add(item) = self.Conditions.Add(item)
        member this.Clear() = self.Conditions.Clear()
        member this.Contains(item) = self.Conditions.Contains(item)
        member this.CopyTo(conditions, index) = self.Conditions.CopyTo(conditions, index)
        member this.Remove(item) = self.Conditions.Remove(item)
        member this.GetEnumerator() = (Seq.cast<Condition> self.Conditions).GetEnumerator()

今、コンパイラは私がコレクションを実装していないと不平を言います。IEnumerable。GetEnumerator()と私はそれを知っていますが、私は本当にそれを行う方法がわかりません。

更新: 最終的な結果は以下の通りです。氏に大きな感謝.ガーランドまた、私はシステムのみを参照していることを指摘する価値があります。コレクション。一般的なシステムを参照するのを忘れていました。非汎用バージョンのIEnumerableが存在する場合のコレクション。悲しいことに、ReSharperはF#をサポートしていません。

open System.Collections
open System.Collections.Generic

type OrCondition() as self =
    inherit Condition()
    member val Conditions = new List<Condition>() with get, set
    interface IList<Condition> with
        member this.Item 
            with get(index) = self.Conditions.[index]
            and  set(index)(value) = self.Conditions.[index] <- value
        member this.IndexOf item = self.Conditions.IndexOf(item)
        member this.Insert(index, item) = self.Conditions.Insert(index, item)
        member this.RemoveAt(index) = self.Conditions.RemoveAt(index)
        member this.Count with get() = self.Conditions.Count
        member this.IsReadOnly with get() = false
        member this.Add(item) = self.Conditions.Add(item)
        member this.Clear() = self.Conditions.Clear()
        member this.Contains(item) = self.Conditions.Contains(item)
        member this.CopyTo(conditions, index) = self.Conditions.CopyTo(conditions, index)
        member this.Remove(item) = self.Conditions.Remove(item)
        member this.GetEnumerator() = self.Conditions.GetEnumerator() :> IEnumerator<Condition>
        member this.GetEnumerator() = self.Conditions.GetEnumerator() :> IEnumerator 
役に立ちましたか?

解決

これは、次の理由によるものです IList 二つを持っています GetEnumerator 方法、のための1つ IEnumerable<T> インターフェイスと非汎用のためのもの IEnumerable インターフェイス。

このメンバーを追加して実装できます IEnumerable

member this.GetEnumerator() = (this.Conditions :> IEnumerable).GetEnumerator()

他のヒント

最も簡単な方法は

です
interface System.Collections.IEnumerable with
    member this.GetEnumerator() = this.Conditions.GetEnumerator() :> _
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top