如何简化向泛型类传递参数

问题描述

我有一个通用类,它将根据指定的任何属性对数据对象进行排序。它按 Ascii 或字母数字排序,升序或降序。基于也在别处的代码 here。 我想做的是简化这个调用。我必须使用

Data.sort(New PropertyComparer(Of Plane)("TailNumber",PropertyComparer(Of Plane).sortDirectionType.Ascending,PropertyComparer(Of Plane).sortTypeType.AlphaNumeric))

有没有办法减少传入的SortDirectionType和SortTypeType参数的长度?

Sub TestPropertySort()
    Dim Data = GetData()
    Data.sort(New PropertyComparer(Of Plane)("TailNumber",PropertyComparer(Of Plane).sortTypeType.AlphaNumeric))
    print(Data,"Plane sort")
End Sub

Private Function GetData() As List(Of Plane)
    Dim Planes As New List(Of Plane)
    Planes.Add(New Plane("81192","Shenyang J-8II"))
    Planes.Add(New Plane("30+91","Eurofighter typhoon"))
    Planes.Add(New Plane("084","General Dynamics F-16 fighting Falcon"))
    Planes.Add(New Plane("83","Test3"))
    Planes.Add(New Plane("A16-97","Lockheed Hudson"))
    Planes.Add(New Plane("L9162","Avro Anson"))
    Planes.Add(New Plane("CH-06","Lockheed C-130 Hercules"))
    Planes.Add(New Plane("CC-06","Test 2"))
    Planes.Add(New Plane("967","Test 1"))
    Planes.Add(New Plane("966","CASA C-212 Aviocar 300DF"))
    Return Planes
End Function

Public Class PropertyComparer(Of T)
    Implements IComparer(Of T)
    Public Enum SortDirectionType
        Descending = -1
        Ascending = 1
    End Enum
    Public Enum SortTypeType
        Ascii
        AlphaNumeric
    End Enum
    Private SortPropertyName As String = ""
    Private SortDirection As SortDirectionType
    Private SortType As SortTypeType
    Public Sub New(ByVal PropertyName As String)
        SortPropertyName = PropertyName
        SortDirection = SortDirectionType.Ascending
        Me.sortType = SortTypeType.Ascii
    End Sub
    Public Sub New(ByVal PropertyName As String,ByVal Direction As SortDirectionType)
        SortPropertyName = PropertyName
        SortDirection = Direction
        Me.sortType = SortTypeType.Ascii
    End Sub
    Public Sub New(ByVal PropertyName As String,ByVal Direction As SortDirectionType,ByVal SortType As SortTypeType)
        SortPropertyName = PropertyName
        SortDirection = Direction
        Me.sortType = SortType
    End Sub

    Public Function Compare(ByVal x As T,ByVal y As T) As Integer Implements System.Collections.Generic.
      IComparer(Of T).Compare
        'The first thing Compare does is get the PropertyInfo record for the x and y arguments
        Dim PropertyX As PropertyInfo = x.GetType().GetProperty(SortPropertyName)
        If PropertyX Is nothing Then Throw New Exception("Sorting on property '" & SortPropertyName & "' bt that property does not exist in the class")
        Dim PropertyY As PropertyInfo = y.GetType().GetProperty(SortPropertyName)
        If PropertyX Is nothing Then Throw New Exception("Sorting on property '" & SortPropertyName & "' bt that property does not exist in the class")
        'Obtain the value of the property using the argument's x and y and the PropertyInfo record

        Dim px As Object = PropertyX.GetValue(x,nothing)
        Dim py As Object = PropertyY.GetValue(y,nothing)

        If SortType = SortTypeType.AlphaNumeric Then
            Return CompareAlphaNumeric(Of String)(CType(px,String),CType(py,String)) * SortDirection
        Else
            'Inspects the type of the property to determine how to call the Compare method implemented at the end of the class. 
            'The Compare method implemented has a where predicate that limits the types of the parameter K to those that implement IComparable. 
            'The reason for this Is that IComparable types implement Compareto.
            If (TypeOf px Is Integer) Then Return Compare(Of Integer)(CType(px,Integer),Integer)) * SortDirection
            If (TypeOf px Is Decimal) Then Return Compare(Of Decimal)(CType(px,Decimal),Decimal)) * SortDirection
            If (TypeOf px Is Date) Then Return Compare(Of Date)(CType(px,Date),Date)) * SortDirection
            If (TypeOf px Is Double) Then Return Compare(Of Double)(CType(px,Double),Double)) * SortDirection
            If (TypeOf px Is String) Then Return Compare(Of String)(CType(px,String)) * SortDirection
        End If

        Dim methodX As MethodInfo = PropertyX.GetType().getmethod("Compareto")
        If (methodX Is nothing = False) Then
            Return CType(methodX.Invoke(px,New Object() {py}),Integer) * SortDirection
        Else
            Return 0
        End If
    End Function
    Private Function Compare(Of K As IComparable)(ByVal x As K,ByVal y As K) As Integer
        Return x.Compareto(y)
    End Function

    Public Function CompareAlphaNumeric(Of K As IComparable)(ByVal X As String,ByVal Y As String) As Integer
        'Validate the arguments.
        If X = nothing Then Return 0
        If Y = nothing Then Return 0

        Dim LengthX As Integer = X.Length
        Dim LengthY As Integer = Y.Length
        Dim Marker1 As Integer = 0
        Dim Marker2 As Integer = 0

        'Loop over both Strings.
        While Marker1 < LengthX And Marker2 < LengthY
            Dim Char1 As Char = X(Marker1)
            Dim Char2 As Char = Y(Marker2)
            Dim TempX(LengthX) As Char
            Dim IndexX As Integer = 0
            Dim TempY(LengthY) As Char
            Dim IndexY As Integer = 0

            'Collect digits for String one.
            Do
                TempX(IndexX) = Char1
                IndexX += 1
                Marker1 += 1

                If Marker1 < LengthX Then
                    Char1 = X(Marker1)
                Else
                    Exit Do
                End If
            Loop While Char.IsDigit(Char1) = Char.IsDigit(TempX(0))

            'Collect digits for String two.
            Do
                TempY(IndexY) = Char2
                IndexY += 1
                Marker2 += 1

                If Marker2 < LengthY Then
                    Char2 = Y(Marker2)
                Else
                    Exit Do
                End If
            Loop While Char.IsDigit(Char2) = Char.IsDigit(TempY(0))

            'Convert to Strings.
            Dim StrX = New String(TempX)
            Dim StrY = New String(TempY)

            'Parse Strings into Integers.
            Dim Result As Integer
            If Char.IsDigit(TempX(0)) And Char.IsDigit(TempY(0)) Then
                Dim ThisNumericChunk = Integer.Parse(StrX)
                Dim ThatNumericChunk = Integer.Parse(StrY)
                Result = ThisNumericChunk.Compareto(ThatNumericChunk)
            Else
                Result = StrX.Compareto(StrY)
            End If

            'Return result if not equal.
            If Not Result = 0 Then
                Return Result
            End If
        End While

        'Compare lengths.
        Return LengthX - LengthY
    End Function
End Class

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)