您的位置:首页 > 编程语言 > VB

IComparer(Of T) 接口的实现 (vb.net)

2008-11-04 23:36 399 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: