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
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
相关文章推荐
- VB.NET使用接口实现附带插件功能的程序
- VB.net和c#利用IOleCommandTarget接口实现Html编辑器的Glyphs(原创)
- VB.NET中实现IEnumerator接口
- 在VB.NET动态接口是如何实现的?
- [VB.NET]VB与MATLAB接口的实现
- 介绍在VB.NET中接口实现多态实际代码知识
- Vb.Net实现ICommand接口,或继承BaseCommand
- VB.NET面向对象的实现(二)
- VB.NET面向对象的实现(十)
- VB.NET实现的MD5加密算法示例【32位】
- [Asp.net 5] Configuration-新一代的配置文件(接口定义与基础实现)
- 章鱼哥—VB.NET 如何实现代码自动生成控件 添加绑定事件
- vb.net 接口(interface)
- VB.Net实现类似C#中continue的功能
- 搭建你的Spring.Net+Nhibernate+Asp.Net Mvc 框架 (三)实现数据库接口层和业务逻辑层
- VB.NET中实现"关机/休眠/重启/注销"的类
- 浅谈VB.NET中的接口
- 利用VB.NET Format函数实现四舍五入功能
- Effective C# Item26:使用IComarable和IComparer接口实现排序关系
- 利用VB.NET Format函数实现四舍五入功能