VisualBasic order by Property by String

时间:2014-08-15 12:28:46

标签: .net vb.net sorting reflection system.reflection

我有一个自定义类的列表,并希望通过属性订购此列表,但我只有属性名称的字符串,因为我从javascript中读取了这个。 我用System.Reflection尝试了这个,但似乎它没有用:

Dim l As List(Of ActionElement) = New List(Of ActionElement)
'Fill the list
l = l.OrderBy(Function(x) GetType(ActionElement).GetProperty("Designation")).ToList()

1 个答案:

答案 0 :(得分:3)

这会让你得到你想要的东西:

Dim l As List(Of ActionElement) = New List(Of ActionElement)
'Fill the list
l = l.OrderBy(Function(x) GetType(ActionElement).GetProperty("Designation").GetValue(x).ToString()).ToList()

我会略微改变它以减少操作:

Dim l As List(Of ActionElement) = New List(Of ActionElement)
Dim sortProperty as PropertyInfo = GetType(ActionElement).GetProperty("Designation")

'Fill the list
l = l.OrderBy(Function(x) sortProperty.GetValue(x).ToString()).ToList()