Custom Collection


SUBMITTED BY: Guest

DATE: Aug. 5, 2014, 9:11 p.m.

FORMAT: C#

SIZE: 3.7 kB

HITS: 24315

  1. Imports System.Collections.ObjectModel
  2. Public Class PluginsCollection
  3. Implements IList(Of IPlugin)
  4. Dim _list As New List(Of IPlugin)
  5. Dim _dict As New Dictionary(Of String, IPlugin)
  6. Public Sub Add(item As IPlugin) Implements ICollection(Of IPlugin).Add
  7. _list.Add(item)
  8. End Sub
  9. Public Sub Add(item As IPlugin, key As String)
  10. _list.Add(item)
  11. _dict.Add(key, item)
  12. End Sub
  13. Public Sub Clear() Implements ICollection(Of IPlugin).Clear
  14. _list.Clear()
  15. _dict.Clear()
  16. End Sub
  17. Public Function Contains(item As IPlugin) As Boolean Implements ICollection(Of IPlugin).Contains
  18. Return _list.Contains(item)
  19. End Function
  20. Public Function ContainsKey(key As String) As Boolean
  21. Return _dict.ContainsKey(key)
  22. End Function
  23. Public ReadOnly Property Count As Integer Implements ICollection(Of IPlugin).Count
  24. Get
  25. Return _list.Count
  26. End Get
  27. End Property
  28. Public Function Remove(item As IPlugin) As Boolean Implements ICollection(Of IPlugin).Remove
  29. Try
  30. _list.Remove(item)
  31. If _dict.ContainsValue(item) Then
  32. _dict.Remove(_dict.FirstOrDefault(Function(x) x.Value Is item).Key)
  33. End If
  34. Return True
  35. Catch ex As Exception
  36. Return False
  37. End Try
  38. End Function
  39. Public Function Remove(key As String)
  40. Try
  41. Dim j = _dict.Item(key)
  42. _list.Remove(j)
  43. _dict.Remove(key)
  44. Return True
  45. Catch ex As Exception
  46. Return False
  47. End Try
  48. End Function
  49. Public Function GetEnumerator() As IEnumerator(Of IPlugin) Implements IEnumerable(Of IPlugin).GetEnumerator
  50. Return _list.GetEnumerator
  51. End Function
  52. Default Public Property Item(index As Integer) As IPlugin Implements IList(Of IPlugin).Item
  53. Get
  54. Try
  55. Return _list(index)
  56. Catch ex As Exception
  57. Return Nothing
  58. End Try
  59. End Get
  60. Set(value As IPlugin)
  61. ' do nothing
  62. End Set
  63. End Property
  64. Default Public Property Item(key As String) As IPlugin
  65. Get
  66. If _dict.ContainsKey(key) Then
  67. Return _dict(key)
  68. Else
  69. Return Nothing
  70. End If
  71. End Get
  72. Set(value As IPlugin)
  73. ' do nothing
  74. End Set
  75. End Property
  76. #Region "Unused code"
  77. Private Sub RemoveAt(index As Integer) Implements IList(Of IPlugin).RemoveAt
  78. '_list.RemoveAt(index)
  79. End Sub
  80. Private Sub CopyTo(array() As IPlugin, arrayIndex As Integer) Implements ICollection(Of IPlugin).CopyTo
  81. '
  82. End Sub
  83. Private Function IndexOf(item As IPlugin) As Integer Implements IList(Of IPlugin).IndexOf
  84. Return _list.IndexOf(item)
  85. End Function
  86. Private Sub Insert(index As Integer, item As IPlugin) Implements IList(Of IPlugin).Insert
  87. '
  88. End Sub
  89. Private Function GetEnumerator1() As IEnumerator Implements IEnumerable.GetEnumerator
  90. Return MyClass.GetEnumerator
  91. End Function
  92. Private ReadOnly Property IsReadOnly As Boolean Implements ICollection(Of IPlugin).IsReadOnly
  93. Get
  94. Return False
  95. End Get
  96. End Property
  97. #End Region
  98. End Class

comments powered by Disqus