Author:水如烟
Namespace
LzmTW.uSystem.uCollection
Partial Class BaseTypeCompare
Public Shared Function IsValid( Of T)( ByVal target As T, ByVal operate As CompareOperate, ByVal a As Object ) As Boolean
Dim mResult As Boolean = False
Select Case operate
Case CompareOperate.In, CompareOperate.NotIn
If a.GetType.IsArray Then
Dim tmp As Object () = CType ( CType (a, Array), [ Object ]())
mResult = IsValidForIn( Of T)(target, operate, tmp)
Else
mResult = IsValidForIn( Of T)(target, operate, New Object () {a})
End If
Case Else
mResult = IsValidForSimple( Of T)(target, operate, a)
End Select
Return mResult
End Function
Public Shared Function IsValid( Of T)( ByVal target As T, ByVal operate As CompareOperate, ByVal a As Object , ByVal b As Object ) As Boolean
Dim mResult As Boolean = False
Select Case operate
Case CompareOperate.Between, CompareOperate.NotBetween
mResult = IsValidForBetween( Of T)(target, operate, a, b)
Case CompareOperate.In, CompareOperate.NotIn
mResult = IsValidForIn( Of T)(target, operate, a, b)
Case Else
End Select
Return mResult
End Function
Public Shared Function IsValid( Of T)( ByVal target As T, ByVal operate As CompareOperate, ByVal ParamArray values() As Object ) As Boolean
Dim mResult As Boolean = False
Select Case operate
Case CompareOperate.Between, CompareOperate.NotBetween
If values.Length = 2 Then
mResult = IsValidForBetween( Of T)(target, operate, values( 0 ), values( 1 ))
End If
Case CompareOperate.In, CompareOperate.NotIn
mResult = IsValidForIn( Of T)(target, operate, values)
Case Else
End Select
Return mResult
End Function
' '' <summary>
' '' Between Or NotBetween
' '' </summary>
Private Shared Function IsValidForBetween( Of T)( ByVal target As T, ByVal operate As CompareOperate, ByVal a As Object , ByVal b As Object ) As Boolean
If Not LzmTW.uRuntimeHelper.IsBaseType(target) Then
Return False
End If
Dim aValue As Object
Try
aValue = LzmTW.uRuntimeHelper.Converter( Of Object , T)(a)
Catch ex As Exception
Return False
End Try
Dim bValue As Object
Try
bValue = LzmTW.uRuntimeHelper.Converter( Of Object , T)(b)
Catch ex As Exception
Return False
End Try
Dim mResult As Boolean
Dim mOperateValid As Boolean
mResult = InternalCompareForBetween(mOperateValid, target, operate, aValue, bValue)
Return mOperateValid And mResult
End Function
' '' <summary>
' '' In or NotIn
' '' </summary>
Private Shared Function IsValidForIn( Of T)( ByVal target As T, ByVal operate As CompareOperate, ByVal ParamArray values() As Object ) As Boolean
If Not LzmTW.uRuntimeHelper.IsBaseType(target) Then
Return False
End If
Dim mValues( - 1 ) As Object
For Each obj As Object In values
Try
Dim tmp As Object = LzmTW.uRuntimeHelper.ChangeType(obj, target)
CommonFunction.Append( Of Object )(mValues, tmp)
Catch ex As Exception
End Try
Next
If mValues.Length = 0 Then
Return False
End If
Dim mResult As Boolean
Dim mOperateValid As Boolean
mResult = InternalCompareForIn(mOperateValid, target, operate, mValues)
Return mOperateValid And mResult
End Function
' '' <summary>
' '' Simple Compare
' '' </summary>
Private Shared Function IsValidForSimple( Of T)( ByVal target As T, ByVal operate As CompareOperate, ByVal a As Object ) As Boolean
If Not LzmTW.uRuntimeHelper.IsBaseType(target) Then
Return False
End If
Dim aValue As T
Select Case operate
Case CompareOperate.Like, CompareOperate.NotLike
Case Else
Try
aValue = LzmTW.uRuntimeHelper.Converter( Of Object , T)(a)
Catch ex As Exception
Return False
End Try
End Select
Dim mResult As Boolean
Dim mOperateValid As Boolean
Select Case operate
Case CompareOperate.Like, CompareOperate.NotLike
mResult = InternalCompareForSimple(mOperateValid, target, operate, a)
Case Else
mResult = InternalCompareForSimple(mOperateValid, target, operate, aValue)
End Select
Return mOperateValid And mResult
End Function
End Class
End Namespace
Partial Class BaseTypeCompare
Public Shared Function IsValid( Of T)( ByVal target As T, ByVal operate As CompareOperate, ByVal a As Object ) As Boolean
Dim mResult As Boolean = False
Select Case operate
Case CompareOperate.In, CompareOperate.NotIn
If a.GetType.IsArray Then
Dim tmp As Object () = CType ( CType (a, Array), [ Object ]())
mResult = IsValidForIn( Of T)(target, operate, tmp)
Else
mResult = IsValidForIn( Of T)(target, operate, New Object () {a})
End If
Case Else
mResult = IsValidForSimple( Of T)(target, operate, a)
End Select
Return mResult
End Function
Public Shared Function IsValid( Of T)( ByVal target As T, ByVal operate As CompareOperate, ByVal a As Object , ByVal b As Object ) As Boolean
Dim mResult As Boolean = False
Select Case operate
Case CompareOperate.Between, CompareOperate.NotBetween
mResult = IsValidForBetween( Of T)(target, operate, a, b)
Case CompareOperate.In, CompareOperate.NotIn
mResult = IsValidForIn( Of T)(target, operate, a, b)
Case Else
End Select
Return mResult
End Function
Public Shared Function IsValid( Of T)( ByVal target As T, ByVal operate As CompareOperate, ByVal ParamArray values() As Object ) As Boolean
Dim mResult As Boolean = False
Select Case operate
Case CompareOperate.Between, CompareOperate.NotBetween
If values.Length = 2 Then
mResult = IsValidForBetween( Of T)(target, operate, values( 0 ), values( 1 ))
End If
Case CompareOperate.In, CompareOperate.NotIn
mResult = IsValidForIn( Of T)(target, operate, values)
Case Else
End Select
Return mResult
End Function
' '' <summary>
' '' Between Or NotBetween
' '' </summary>
Private Shared Function IsValidForBetween( Of T)( ByVal target As T, ByVal operate As CompareOperate, ByVal a As Object , ByVal b As Object ) As Boolean
If Not LzmTW.uRuntimeHelper.IsBaseType(target) Then
Return False
End If
Dim aValue As Object
Try
aValue = LzmTW.uRuntimeHelper.Converter( Of Object , T)(a)
Catch ex As Exception
Return False
End Try
Dim bValue As Object
Try
bValue = LzmTW.uRuntimeHelper.Converter( Of Object , T)(b)
Catch ex As Exception
Return False
End Try
Dim mResult As Boolean
Dim mOperateValid As Boolean
mResult = InternalCompareForBetween(mOperateValid, target, operate, aValue, bValue)
Return mOperateValid And mResult
End Function
' '' <summary>
' '' In or NotIn
' '' </summary>
Private Shared Function IsValidForIn( Of T)( ByVal target As T, ByVal operate As CompareOperate, ByVal ParamArray values() As Object ) As Boolean
If Not LzmTW.uRuntimeHelper.IsBaseType(target) Then
Return False
End If
Dim mValues( - 1 ) As Object
For Each obj As Object In values
Try
Dim tmp As Object = LzmTW.uRuntimeHelper.ChangeType(obj, target)
CommonFunction.Append( Of Object )(mValues, tmp)
Catch ex As Exception
End Try
Next
If mValues.Length = 0 Then
Return False
End If
Dim mResult As Boolean
Dim mOperateValid As Boolean
mResult = InternalCompareForIn(mOperateValid, target, operate, mValues)
Return mOperateValid And mResult
End Function
' '' <summary>
' '' Simple Compare
' '' </summary>
Private Shared Function IsValidForSimple( Of T)( ByVal target As T, ByVal operate As CompareOperate, ByVal a As Object ) As Boolean
If Not LzmTW.uRuntimeHelper.IsBaseType(target) Then
Return False
End If
Dim aValue As T
Select Case operate
Case CompareOperate.Like, CompareOperate.NotLike
Case Else
Try
aValue = LzmTW.uRuntimeHelper.Converter( Of Object , T)(a)
Catch ex As Exception
Return False
End Try
End Select
Dim mResult As Boolean
Dim mOperateValid As Boolean
Select Case operate
Case CompareOperate.Like, CompareOperate.NotLike
mResult = InternalCompareForSimple(mOperateValid, target, operate, a)
Case Else
mResult = InternalCompareForSimple(mOperateValid, target, operate, aValue)
End Select
Return mOperateValid And mResult
End Function
End Class
End Namespace
Namespace
LzmTW.uSystem.uCollection
' '' <summary>
' '' 两数据比较。数据类型以a为参考。
' '' </summary>
' '' <remarks></remarks>
Public Class BaseTypeCompare
Private Shared gCompare As Comparer
Private Shared gErrors As ArrayList
Public ReadOnly Property Errors() As ArrayList
Get
Return gErrors
End Get
End Property
Public Shared Sub ClearErrors()
gErrors.Clear()
End Sub
Public Shared ReadOnly Property HasErrors() As Boolean
Get
Return gErrors.Count > 0
End Get
End Property
Shared Sub New ()
gCompare = New Comparer(System.Globalization.CultureInfo.CurrentCulture)
gErrors = New ArrayList
End Sub
Private Shared Function InternalCompare( ByRef OperateValid As Boolean , ByVal target As Object , ByVal operate As CompareOperate, ByVal whereDefines() As KeyValuePair( Of String , Object )) As Boolean
Dim mResult As Boolean = False
Try
Throw New Exception( String .Format( " {0} 操作无效 " , operate.ToString))
Select Case operate
Case CompareOperate.Where
Case Else
End Select
OperateValid = True
Catch ex As Exception
OperateValid = False
AddError(ex.ToString)
End Try
Return mResult
End Function
Private Shared Function InternalCompareForIn( ByRef OperateValid As Boolean , ByVal target As Object , ByVal operate As CompareOperate, ByVal values() As Object ) As Boolean
Dim mResult As Boolean = False
Try
Select Case operate
Case CompareOperate.In
mResult = Array.IndexOf(values, target) <> - 1
Case CompareOperate.NotIn
mResult = Array.IndexOf(values, target) = - 1
Case Else
Throw New Exception( String .Format( " {0} 操作无效 " , operate.ToString))
End Select
OperateValid = True
Catch ex As Exception
OperateValid = False
AddError(ex.ToString)
End Try
Return mResult
End Function
Private Shared Function InternalCompareForBetween( ByRef OperateValid As Boolean , ByVal target As Object , ByVal operate As CompareOperate, ByVal a As Object , ByVal b As Object ) As Boolean
Dim mResult As Boolean = False
Try
Select Case operate
Case CompareOperate.Between
mResult = (gCompare.Compare(target, a) >= 0 ) AndAlso (gCompare.Compare(target, b) <= 0 )
Case CompareOperate.NotBetween
mResult = (gCompare.Compare(target, a) = - 1 ) OrElse (gCompare.Compare(target, b) = 1 )
Case Else
Throw New Exception( String .Format( " {0} 操作无效 " , operate.ToString))
End Select
OperateValid = True
Catch ex As Exception
OperateValid = False
AddError(ex.ToString)
End Try
Return mResult
End Function
Private Shared Function InternalCompareForSimple( ByRef OperateValid As Boolean , ByVal target As Object , ByVal operate As CompareOperate, ByVal a As Object ) As Boolean
Dim mResult As Boolean = False
Try
Select Case operate
Case CompareOperate.Equal
mResult = (gCompare.Compare(target, a) = 0 )
Case CompareOperate.NotEqual
mResult = (gCompare.Compare(target, a) <> 0 )
Case CompareOperate.Greater
mResult = (gCompare.Compare(target, a) = 1 )
Case CompareOperate.GreaterEqual
mResult = (gCompare.Compare(target, a) >= 0 )
Case CompareOperate.Less
mResult = (gCompare.Compare(target, a) = - 1 )
Case CompareOperate.LessEqual
mResult = (gCompare.Compare(target, a) <= 0 )
Case CompareOperate.Like
mResult = (target.ToString Like a.ToString)
Case CompareOperate.NotLike
mResult = Not (target.ToString Like a.ToString)
Case Else
Throw New Exception( String .Format( " {0} 操作无效 " , operate.ToString))
End Select
OperateValid = True
Catch ex As Exception
OperateValid = False
AddError(ex.ToString)
End Try
Return mResult
End Function
Friend Shared Sub AddError( ByVal msg As String )
gErrors.Add(msg)
End Sub
End Class
End Namespace
' '' <summary>
' '' 两数据比较。数据类型以a为参考。
' '' </summary>
' '' <remarks></remarks>
Public Class BaseTypeCompare
Private Shared gCompare As Comparer
Private Shared gErrors As ArrayList
Public ReadOnly Property Errors() As ArrayList
Get
Return gErrors
End Get
End Property
Public Shared Sub ClearErrors()
gErrors.Clear()
End Sub
Public Shared ReadOnly Property HasErrors() As Boolean
Get
Return gErrors.Count > 0
End Get
End Property
Shared Sub New ()
gCompare = New Comparer(System.Globalization.CultureInfo.CurrentCulture)
gErrors = New ArrayList
End Sub
Private Shared Function InternalCompare( ByRef OperateValid As Boolean , ByVal target As Object , ByVal operate As CompareOperate, ByVal whereDefines() As KeyValuePair( Of String , Object )) As Boolean
Dim mResult As Boolean = False
Try
Throw New Exception( String .Format( " {0} 操作无效 " , operate.ToString))
Select Case operate
Case CompareOperate.Where
Case Else
End Select
OperateValid = True
Catch ex As Exception
OperateValid = False
AddError(ex.ToString)
End Try
Return mResult
End Function
Private Shared Function InternalCompareForIn( ByRef OperateValid As Boolean , ByVal target As Object , ByVal operate As CompareOperate, ByVal values() As Object ) As Boolean
Dim mResult As Boolean = False
Try
Select Case operate
Case CompareOperate.In
mResult = Array.IndexOf(values, target) <> - 1
Case CompareOperate.NotIn
mResult = Array.IndexOf(values, target) = - 1
Case Else
Throw New Exception( String .Format( " {0} 操作无效 " , operate.ToString))
End Select
OperateValid = True
Catch ex As Exception
OperateValid = False
AddError(ex.ToString)
End Try
Return mResult
End Function
Private Shared Function InternalCompareForBetween( ByRef OperateValid As Boolean , ByVal target As Object , ByVal operate As CompareOperate, ByVal a As Object , ByVal b As Object ) As Boolean
Dim mResult As Boolean = False
Try
Select Case operate
Case CompareOperate.Between
mResult = (gCompare.Compare(target, a) >= 0 ) AndAlso (gCompare.Compare(target, b) <= 0 )
Case CompareOperate.NotBetween
mResult = (gCompare.Compare(target, a) = - 1 ) OrElse (gCompare.Compare(target, b) = 1 )
Case Else
Throw New Exception( String .Format( " {0} 操作无效 " , operate.ToString))
End Select
OperateValid = True
Catch ex As Exception
OperateValid = False
AddError(ex.ToString)
End Try
Return mResult
End Function
Private Shared Function InternalCompareForSimple( ByRef OperateValid As Boolean , ByVal target As Object , ByVal operate As CompareOperate, ByVal a As Object ) As Boolean
Dim mResult As Boolean = False
Try
Select Case operate
Case CompareOperate.Equal
mResult = (gCompare.Compare(target, a) = 0 )
Case CompareOperate.NotEqual
mResult = (gCompare.Compare(target, a) <> 0 )
Case CompareOperate.Greater
mResult = (gCompare.Compare(target, a) = 1 )
Case CompareOperate.GreaterEqual
mResult = (gCompare.Compare(target, a) >= 0 )
Case CompareOperate.Less
mResult = (gCompare.Compare(target, a) = - 1 )
Case CompareOperate.LessEqual
mResult = (gCompare.Compare(target, a) <= 0 )
Case CompareOperate.Like
mResult = (target.ToString Like a.ToString)
Case CompareOperate.NotLike
mResult = Not (target.ToString Like a.ToString)
Case Else
Throw New Exception( String .Format( " {0} 操作无效 " , operate.ToString))
End Select
OperateValid = True
Catch ex As Exception
OperateValid = False
AddError(ex.ToString)
End Try
Return mResult
End Function
Friend Shared Sub AddError( ByVal msg As String )
gErrors.Add(msg)
End Sub
End Class
End Namespace