- Imports Microsoft.VisualBasic
- Imports System.Collections.Generic
- Imports System.Reflection
- Public Class IListComparer(Of T)
- Implements IComparer(Of T)
- Private mpropertyName As String
- Private mIsAsc As Boolean
- Public Property PropertyName() As String
- Get
- Return mpropertyName
- End Get
- Set(ByVal value As String)
- mpropertyName = value
- End Set
- End Property
- Public Property IsAsc() As Boolean
- Get
- Return mIsAsc
- End Get
- Set(ByVal value As Boolean)
- mIsAsc = value
- End Set
- End Property
- Public Sub New(ByVal propertyName As String, ByVal IsAsc As Boolean)
- mIsAsc = IsAsc
- mpropertyName = propertyName
- End Sub
- Public Function Compare(ByVal x As T, ByVal y As T) As Integer Implements System.Collections.Generic.IComparer(Of T).Compare
- Dim pro As PropertyInfo = GetType(T).GetProperty(Me.PropertyName)
- Dim result As Integer = 0
- Select Case pro.PropertyType.Name.ToUpper
- Case "DATE", "DATETIME"
- result = Date.Compare(pro.GetValue(x, Nothing), pro.GetValue(y, Nothing))
- Case "INT32", "INTEGER", "INT32"
- result = Integer.Parse(pro.GetValue(x, Nothing)).CompareTo(Integer.Parse(pro.GetValue(y, Nothing)))
- Case "DECIMAL"
- result = Decimal.Compare(pro.GetValue(x, Nothing), pro.GetValue(y, Nothing))
- Case Else
- result = String.Compare(pro.GetValue(x, Nothing), pro.GetValue(y, Nothing), True)
- End Select
- If result = 0 Then
- Return result
- ElseIf IsAsc Then
- Return (-1) * result
- End If
- Return result
- End Function
- End Class
IComparer(Of T) 接口的实现 (vb.net)
最新推荐文章于 2015-03-22 21:11:02 发布