2015-07-09 50 views
2

我的自定義集合Example實現了IEnumerable(Of Long)IDictionary(Of String, Long)如何在For Each語句中使用不同的GetEnumerator()

當我列舉通過我的收藏與For Each,我得到的值類型KeyValuePair(Of String, Long),但我想要的值類型Long

三個GetEnumerator的只有返回類型不同。這不是超載。

Module Main 
    Sub Main() 
     Dim Examples As New Example 

     For Each E In Examples 
      ' E is of type KeyValuePair(Of String, Long) 
      ' I want E to be of type Long 
     Next 
    End Sub 
End Module 

Public Class Example 
    Implements IEnumerable(Of Long), IDictionary(Of String, Long) 

    Private Property TheKeyedCollection As KeyedCollection(Of String, Long) 

    Public Function GetEnumerator() As IEnumerator(Of KeyValuePair(Of String, Long)) Implements IEnumerable(Of KeyValuePair(Of String, Long)).GetEnumerator 
     Return TheKeyedCollection.GetEnumerator() 
    End Function 

    Public Function GetEnumerator1() As IEnumerator(Of Long) Implements IEnumerable(Of Long).GetEnumerator 
     Return TheKeyedCollection.GetEnumerator() 
    End Function 

    Public Function GetEnumerator2() As IEnumerator Implements IEnumerable.GetEnumerator 
     Return TheKeyedCollection.GetEnumerator() 
    End Function 


    '>>>>>>>>>>>>>>>>>>>>>>>>>>>> IGNORE THESE OTHER UNNECESSARY FUNCTIONS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
    Private Sub Add(item As KeyValuePair(Of String, Long)) Implements ICollection(Of KeyValuePair(Of String, Long)).Add 
    End Sub 
    Private Sub Clear() Implements ICollection(Of KeyValuePair(Of String, Long)).Clear 
    End Sub 
    Private Function Contains(item As KeyValuePair(Of String, Long)) As Boolean Implements ICollection(Of KeyValuePair(Of String, Long)).Contains 
     Throw New Exception 
    End Function 
    Private Sub CopyTo(array() As KeyValuePair(Of String, Long), arrayIndex As Integer) Implements ICollection(Of KeyValuePair(Of String, Long)).CopyTo 
    End Sub 
    Private ReadOnly Property Count As Integer Implements ICollection(Of KeyValuePair(Of String, Long)).Count 
     Get 
      Throw New Exception 
     End Get 
    End Property 
    Private ReadOnly Property IsReadOnly As Boolean Implements ICollection(Of KeyValuePair(Of String, Long)).IsReadOnly 
     Get 
      Throw New Exception 
     End Get 
    End Property 
    Private Function Remove(item As KeyValuePair(Of String, Long)) As Boolean Implements ICollection(Of KeyValuePair(Of String, Long)).Remove 
     Throw New Exception 
    End Function 
    Private Sub Add1(key As String, value As Long) Implements IDictionary(Of String, Long).Add 
    End Sub 
    Private Function ContainsKey(key As String) As Boolean Implements IDictionary(Of String, Long).ContainsKey 
    End Function 
    '>>>>>>>>>>>>>>>>>>>>>>>>>>>> IGNORE THESE OTHER UNNECESSARY FUNCTIONS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
    Private Property Item(key As String) As Long Implements IDictionary(Of String, Long).Item 
     Get 
      Throw New Exception 
     End Get 
     Set(value As Long) 
     End Set 
    End Property 
    Private ReadOnly Property Keys As ICollection(Of String) Implements IDictionary(Of String, Long).Keys 
     Get 
      Throw New Exception 
     End Get 
    End Property 
    Private Function Remove1(key As String) As Boolean Implements IDictionary(Of String, Long).Remove 
     Throw New Exception 
    End Function 
    Private Function TryGetValue(key As String, ByRef value As Long) As Boolean Implements IDictionary(Of String, Long).TryGetValue 
     Throw New Exception 
    End Function 
    Private ReadOnly Property Values As ICollection(Of Long) Implements IDictionary(Of String, Long).Values 
     Get 
      Throw New Exception 
     End Get 
    End Property 
    '>>>>>>>>>>>>>>>>>>>>>>>>>>>> IGNORE THESE OTHER UNNECESSARY FUNCTIONS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
End Class 

我試着設置GetEnumerator()GetEnumerator2()私人,但所產生的錯誤:'For Each' on type 'Example.Example' is ambiguous because the type implements multiple instantiations of 'System.Collections.Generic.IEnumerable(Of Long)'.

如何挑選其中GetEnumerator()For Each用途?

回答

1

將您希望作爲默認枚舉器的函數的名稱更改爲GetEnumerator()。不是GetEnumerator1()

如果您不打算使用其他兩個版本的GetEnumerator,您可以將它們設置爲Private,但它們仍然必須在那裏以滿足給定的接口。

如果你想使用的GetEnumerator()■這是不是默認的一個,即GetEnumerator1(),你需要強制For Each來推斷它是這樣的:

For Each E In DirectCast(Example, IEnumerable(Of String)) 
    ' E is of type String 
Next