Imports System.Collections
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Reflection
Imports System.ComponentModel.DataAnnotations
Imports Microsoft.VisualBasic
Imports System.ComponentModel
Imports System.Collections.Specialized
Imports System.Collections.ObjectModel
Imports System.Runtime.CompilerServices
Imports Newtonsoft.Json.Linq
Imports Newtonsoft.Json.Converters
Imports Newtonsoft.Json.Serialization
Public Class ObservableCollectionAdvanced(Of T As INotifyPropertyChanged)
Implements ICollection(Of T)
Implements INotifyPropertyChanged
Implements INotifyCollectionChanged
Implements INotifyCollectionItemPropertyChanged
Public Sub Add(Item As T) Implements ICollection(Of T).Add
If Item IsNot Nothing Then
Me.AddHandlerToItem(Item)
Public Function Contains(ByVal item As T) As Boolean Implements ICollection(Of T).Contains
If item IsNot Nothing Then
Return _collection.COntains(item)
Public ReadOnly Property Count() As Integer Implements ICollection(Of T).Count
Public ReadOnly Property IsReadOnly() As Boolean Implements ICollection(Of T).IsReadOnly
Public Sub CopyTo(array As T(), arrayIndex As Integer) Implements ICollection(Of T).CopyTo
_collection.CopyTo(array, arrayIndex)
Public Sub Clear() Implements ICollection(Of T).Clear
Throw New NotImplementedException()
Public Function Remove(ByVal item As T) As Boolean Implements ICollection(Of T).Remove
Throw New NotImplementedException()
Private ReadOnly _collection As New ObservableCollection(Of T)
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Event CollectionChanged As NotifyCollectionChangedEventHandler Implements INotifyCollectionChanged.CollectionChanged
Public Event CollectionItemPropertyChanged As PropertyChangedEventHandler Implements INotifyCollectionItemPropertyChanged.CollectionItemPropertyChanged
AddHandler _collection.CollectionChanged, Sub(sender As Object, e As NotifyCollectionChangedEventArgs)
RaiseEvent CollectionChanged(Me, e)
Default Public ReadOnly Property Item(Index As Integer) As T
If Index >= 0 AndAlso Index < _collection.Count Then
Return _collection(Index)
Private Sub AddHandlerToItem(Item As T)
If Not TypeOf Item Is INotifyPropertyChanged Then Exit Sub
AddHandler DirectCast(Item, INotifyPropertyChanged).PropertyChanged, AddressOf Item_PropertyChanged
Private Sub Item_PropertyChanged(sender As Object, e As PropertyChangedEventArgs)
RaiseEvent CollectionItemPropertyChanged(sender, e)
Public Function GetEnumerator() As IEnumerator(Of T) Implements IEnumerable(Of T).GetEnumerator
Return _collection.GetEnumerator()
Private Function GetEnumerator1() As IEnumerator Implements IEnumerable.GetEnumerator
Return Me.GetEnumerator()
Public Interface INotifyCollectionItemPropertyChanged
Event CollectionItemPropertyChanged As PropertyChangedEventHandler
Inherits INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler _
Implements INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(<CallerMemberName()> Optional ByVal propertyName As String = Nothing)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
Dim devices As New ObservableCollectionAdvanced(Of IDevice)
devices.add(new TorsionArmDevice())
Dim JsonString as string = JsonConvert.SerializeObject(devices, Newtonsoft.Json.Formatting.Indented, New JsonSerializerSettings With {.TypeNameHandling = TypeNameHandling.All})
Console.WriteLine(JsonString)
Dim newDevices = Newtonsoft.Json.JsonConvert.DeserializeObject(of ObservableCollectionAdvanced(Of IDevice))(JsonString, New JsonSerializerSettings With {.TypeNameHandling = TypeNameHandling.All})
Assert.AreEqual(devices.Count, newDevices.Count)
Console.WriteLine("Environment version: " & Environment.Version.ToString())
Console.WriteLine("Json.NET version: " & GetType(JsonSerializer).Assembly.FullName)
Console.WriteLine("Unhandled exception: ")