对对象

时间:2017-11-13 01:09:45

标签: vb.net sorting reflection

我试图对对象的所有List属性进行排序(使用OrderBy)。它是一个复杂的对象,其中包含可能包含列表的列表。我认为反射和递归会让我在那里,但我似乎对实际排序列表感到磕磕绊。

这是我的代码:

Private Sub SortData(ByRef obj As Object)
    Dim type As Type = obj.GetType()
    Dim properties As PropertyInfo() = type.GetProperties()

    For Each item As PropertyInfo In properties
        If Not item.PropertyType.FullName.Contains("System.Collection") AndAlso item.PropertyType.FullName.Contains("MyType") Then
            Dim value = obj.GetType().GetProperty(item.Name).GetValue(obj)
            If value IsNot Nothing Then
                SortData(value)
            End If
        End If

        If item.PropertyType.FullName.Contains("System.Collection") Then
            Dim value = obj.GetType().GetProperty(item.Name).GetValue(obj)
            If value IsNot Nothing Then
                Dim subType As Type = value(0).GetType()
                Dim subProperties As PropertyInfo() = subType.GetProperties()
                If subProperties.Any(Function(x) x.Name = "SortKey") Then
                    ' --- Struggling here! ---
                    Dim castList = TryCast(value, List(Of MoreDetail))
                    value = castList.OrderBy(Function(x) x.SortKey)
                    value = TryCast(value, List(Of MoreDetail))
                    ' --- End struggle ---
                End If

                For Each subProperty In subProperties
                    SortData(value.GetType().GetProperty(subProperty.Name).GetValue(value))
                Next
            End If
        End If
    Next

End Sub

主要问题是在这里挣扎"代码的一部分 - 我似乎必须将对象转换为其原始类型,但这会破坏反射的目的,因为不同的对象将具有不同的列表类型,所以理想情况下我能够基于对象转换关于它的反映类型。

感谢您的帮助!

编辑:缓解" XY问题"在我的问题中,这里是我想要做的...我希望能够对对象的所有List属性进行排序 - 问题是对象可以有嵌套列表,所以理想情况下我&# 39; d通过反射执行此操作,递归检查列表是否包含任何子属性中的列表。列表大多是复杂类型,所以我只想在集合包含具有" SortKey"的对象时才对列表进行排序。属性定义。

编辑2:我之后的一个例子是(伪代码):

Public Class SuperList
    Public Property A As String = ""
    Public Property BList As New List(Of B)
End Class

Public Class B
   Public Property SortKey As String = ""
   Public Property Name As String = ""
   Public Property CList As New List(Of C)
End Class

Public Class C
   Public Property SortKey As String = ""
   Public Property Name As String = ""
End Class

Dim cExampleSecond As New C With {.SortKey = "345", .Name = "C Second"}
Dim cExampleFirst As New C With {.SortKey = "123", .Name = "C First"}
Dim BExampleFirst As New B With {.Sortkey = "ABC", .Name = "B First"}
BExampleFirst.CList.Add(cExampleSecond)
BExampleFirst.CList.Add(cExampleFirst)
Dim superExample As New SuperList With {.A = "Whatever"}
superExample.BList.Add(BExampleFirst)

...
SortData(superExample)
...

运行SortData()之后,它应该如下所示:

superExample = 
   .A = "Whatever"
   .BList = 
      .SortKey = "ABC"
      .BExampleFirst = 
         cExampleFirst = 
            .SortKey = "123" 
            .Name = "C First"
         cExampleSecond = 
            .SortKey = "345" 
            .Name = "C Second"

请注意,如果superExample.BList有更多元素,它们也会被排序......

1 个答案:

答案 0 :(得分:0)

看看你的例子,我认为你想要对一种树进行排序。

1- Tree表示递归:SortData()将对当前级别/当前列表进行排序,并为每个子元素调用SortData()。您可以使用继承或组合在树的每个元素中实现此功能。

2-您可以使用sortList的{​​{1}}功能。您只需要为列表元素实现IComparable。

以下是一些示例代码,用于说明全局方法。

Dictionary