如何在每个语句中使用不同的GetEnumerator()

时间:2015-07-09 23:54:45

标签: vb.net types foreach ienumerable enumerator

我的自定义收藏集Example实施IEnumerable(Of Long)IDictionary(Of String, Long)

当我使用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 个答案:

答案 0 :(得分:1)

将您想要的功能名称更改为GetEnumerator()作为默认枚举器。不是GetEnumerator1()

如果您不打算使用其他两个版本的GetEnumerator,可以将它们设置为Private,但它们仍然必须在那里以满足给定的接口。

如果你想使用其中一个非默认的GetEnumerator(),即GetEnumerator1(),你需要强制For Each推断它:

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