Моя обычная коллекция Example
реализует IEnumerable(Of Long)
и IDictionary(Of String, Long)
.Как использовать другой GetEnumerator() для каждого заявления
Когда я перечисляю свою коллекцию с помощью For Each
, я получаю значения типа KeyValuePair(Of String, Long)
, но мне нужны значения типа Long
.
Только зарегистрированные пользователи имеют отличаются своим возвратным типом. Это не перегрузка.
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
использует?