|
''' <summary>
''' Compares the properties of two generic objects.
''' </summary>
''' <remarks></remarks>
Public Class PropertyComparer
Implements IComparer
Private mSortDescriptions As New List(Of PropertySortDescription)
''' <summary>
''' The sort descriptions for comparing in ordination to this list. If a property is equal with both objects, the next property description in list will be used for comparing until one object differs to the other.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public ReadOnly Property SortDescriptions() As List(Of PropertySortDescription)
Get
Return mSortDescriptions
End Get
End Property
''' <summary>
''' Compares both objects.
''' </summary>
''' <param name="x">First Object</param>
''' <param name="y">Second Object</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
If (x IsNot Nothing And y IsNot Nothing) Then
For Each desc In mSortDescriptions
Dim xValue As Object = desc.Descriptor.GetValue(x)
Dim yValue As Object = desc.Descriptor.GetValue(y)
Dim res As Integer = desc.Direction * desc.Comparer.Compare(xValue, yValue)
If res <> 0 Then Return res
Next
Return 0
Else
Return 0
End If
End Function
End Class
''' <summary>
''' Provides properties for storing reflection information of a generic object.
''' </summary>
''' <remarks></remarks>
Public Class PropertySortDescription
Implements INotifyPropertyChanged
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Private mDescriptor As PropertyDescriptor
Private mDirection As ListSortDirection = ListSortDirection.Ascending
Private mComparer As System.Collections.IComparer
Friend ReadOnly Property Comparer() As System.Collections.IComparer
Get
Return mComparer
End Get
End Property
Public ReadOnly Property Descriptor() As PropertyDescriptor
Get
Return mDescriptor
End Get
End Property
Public Property Direction() As ListSortDirection
Get
Return mDirection
End Get
Set(ByVal value As ListSortDirection)
mDirection = value
End Set
End Property
Public Sub New(ByVal pd As PropertyDescriptor, ByVal dir As ListSortDirection)
Me.New(pd)
mDirection = dir
End Sub
Public Sub New(ByVal pd As PropertyDescriptor)
Me.SetDescriptor(pd)
End Sub
Public Sub New(ByVal prpName As String, ByVal t As Type, ByVal dir As ListSortDirection)
Me.New(prpName, t)
mDirection = dir
End Sub
Public Sub New(ByVal prpName As String, ByVal t As Type)
Dim prps As PropertyDescriptorCollection = TypeDescriptor.GetProperties(t)
For Each prp As PropertyDescriptor In prps
If prp.Name = prpName Then
Me.SetDescriptor(prp)
prps = Nothing
Exit For
End If
Next
If mDescriptor Is Nothing Then Throw New ArgumentException("A property with the passed name couldn't be found on passed object.", "prpName")
End Sub
Private Sub SetDescriptor(ByVal pd As PropertyDescriptor)
If pd Is Nothing Then : Throw New ArgumentNullException("pd", "Property descriptor is not allowed to be null/Nothing.")
Else
Try
mDescriptor = pd
mComparer = DirectCast(GetType(Comparer).MakeGenericType(mDescriptor.PropertyType).InvokeMember("Default", Reflection.BindingFlags.GetProperty Or Reflection.BindingFlags.[Public] Or Reflection.BindingFlags.[Static], Nothing, Nothing, Nothing), System.Collections.IComparer)
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
End If
End Sub
End Class
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.