WithEvents LinkedList is this not possible?

What is the best approach to WithEvents Collection- VB.NET?

Do you have any comments on the code below (skipping checks Nothing)?

The problem is what I get LinkedListNode(Of Foo)in the block For Eachthat I can install myNode.Value = something, and here is the leak of the handlers ...

-Can I override FooCollection GetEnumeratorin this case?
-No. :( reason NotInheritable Class LinkedListNode(Of T)

Class Foo
  Public Event SelectedChanged As EventHandler
End Class

Class FooCollection
  Inherits LinkedList(Of Foo)
  Public Event SelectedChanged As EventHandler

  Protected Overloads Sub AddFirst(ByVal item As Foo)
    AddHandler item.SelectedChanged, AddressOf OnSelectedChanged
    MyBase.AddFirst(item)
  End Sub

  Protected Overloads Sub AddLast(ByVal item As Foo)
    AddHandler item.SelectedChanged, AddressOf OnSelectedChanged
    MyBase.AddLast(item)
  End Sub

  ' ------------------- '

  Protected Overloads Sub RemoveFirst()
    RemoveHandler MyBase.First.Value.SelectedChanged, _
                         AddressOf OnSelectedChanged
    MyBase.RemoveFirst()
  End Sub

  Protected Overloads Sub RemoveLast(ByVal item As Foo)
    RemoveHandler MyBase.Last.Value.SelectedChanged, _
                        AddressOf OnSelectedChanged
    MyBase.RemoveLast()
  End Sub

  ' ------------------- '

  Protected Sub OnSelectedChanged(ByVal sender As Object, ByVal e As EventArgs)
    RaiseEvent SelectedChanged(sender, e)
  End Sub
End Class
+3
source share
3 answers

, GetEnumerator. , LinkedList, :

Class FooCollection
    Implements ICollection(Of Foo)

    Private list As New LinkedList(Of Foo)

    Public Sub AddFirst(ByVal item As Foo)
        AddHandler item.SelectedChanged, AddressOf OnSelectedChanged
        list.AddFisrt(item)
    End Sub

    ...

End Class

, , , .

+1

CollectionChangedEventManager? , , .

+1

, , ocjects , , .

, :

1:) , . MSDN . "ObservableNotifiableCollection".

2:) , , ( PropertyChangedEvent ), -. : " InotifyPropertyChanged Castle.DynamicProxy".

0

Source: https://habr.com/ru/post/1735576/


All Articles