问题描述
此功能使用泡沫算法,通过他们的名称
属性 IO.DirectoryInfo
的列表进行排序。
我怎样才能在一个参数中指定,我将到列表排序的属性?
例如:驱动器,姓名,Name.Length,Directory.Parent,等等...
我觉得是个好主意(也许是不好的,我不知道有多少可以改善这一点)是传递参数为字符串,然后投字符串...?这里是我迷路了。
公共共享功能BubbleSort_List(名单列表(IO.DirectoryInfo),BYVAL SortByProperty为...)作为目录(中IO.DirectoryInfo)
返回list.Select(功能(S)新增功能{_
键.OrgStr = S,_
主要.SortStr = System.Text.RegularEx pressions.Regex.Replace(_
s.Name,(\ D +)|(\ d +),_
功能(M)m.Value.PadLeft(list.Select(功能(文件夹)folder.Name.Length)的.max,_
如果(Char.IsDigit(m.Value(0)),C,Char.MaxValue)))_
})排序依据(功能(X)x.SortStr)。选择(功能(X)x.OrgStr).ToList
端功能
更新
注意这部分的code以上:
list.Select(功能(文件夹)folder.Name.Length)的.max
我需要的是调用函数指定我想,而不是名属性的属性。
尝试使用@Sriram Sakthivel解决方案,但它抛出一个异常约之间的UnaryEx pression不兼容铸造MemberEx pression的[属性]变量。
进口的System.Reflection
进口System.Linq.Ex pressions
私人小组测试(发送者为对象,E作为EventArgs的)把手MyBase.Shown
在这里,我创建列表
昏暗的文件夹以列表(中IO.DirectoryInfo)= _
IO.Directory.GetDirectories(E:\曲艺\ Canciones,*,IO.SearchOption.TopDirectoryOnly)_
。选择(功能(p)的新IO.DirectoryInfo(P))。了ToList()
在这里,我尝试循环列表中,同时我尝试排序,
指定我想用@Sriram Sakthivel解决方案的性能,
这部分不工作,因为第二parametter是错误的。
对于每个folderinfo在BubbleSort_List(文件夹功能()名称)
MSGBOX(folderinfo.Name)
下一个
结束小组
专用功能BubbleSort_List(名单列表(IO.DirectoryInfo),EXP作为防爆pression(中Func键(对象)))作为目录(中IO.DirectoryInfo)
昏暗的[属性]作为的PropertyInfo = DirectCast(DirectCast(exp.Body,MemberEx pression)。成员,的PropertyInfo)
返回list.Select(功能(S)新增功能{_
键.OrgStr = S,_
主要.SortStr = System.Text.RegularEx pressions.Regex.Replace(_
s.Name,(\ D +)|(\ d +),_
功能(M)m.Value.PadLeft(list.Select(功能(文件夹)DirectCast([属性] .GetValue(文件夹中,为Nothing),字符串).Length)的.max(),_
如果(Char.IsDigit(m.Value(0)),C,Char.MaxValue)))_
})排序依据(功能(X)x.SortStr)。选择(功能(X)x.OrgStr).ToList
端功能
如果我理解你想要正确的东西,在斯利拉姆Sakthivel $ C $账套需要什么部分,但不能提供你想要的。
对于每个folderinfo在BubbleSort_List(文件夹中的姓名)
MSGBOX(folderinfo.Name)
下一个
您必须设置与目标属性(名称,CREATIONTIME等)的名称的字符串类型的参数,和检索列表中的项目之一,这个属性(第一个,例如)通过的getProperty
;记住,LINQ查询指的是物品,而不是整个列表。
专用功能BubbleSort_List(名单列表(IO.DirectoryInfo),作为propName作为字符串)作为目录(中IO.DirectoryInfo)
昏暗curProperty作为的PropertyInfo =列表(0).GetType()的getProperty(作为propName)
返回list.Select(功能(S)新增功能{_
键.OrgStr = S,_
主要.SortStr = System.Text.RegularEx pressions.Regex.Replace(_
s.Name,(\ D +)|(\ d +),_
功能(M)m.Value.PadLeft(list.Select(功能(文件夹)DirectCast(curProperty.GetValue(文件夹中,为Nothing),字符串).Length)的.max(),_
如果(Char.IsDigit(m.Value(0)),C,Char.MaxValue)))_
})排序依据(功能(X)x.SortStr)。选择(功能(X)x.OrgStr).ToList
端功能
注:我只是建议您code进行校正,使你得到你想要我的理解它。我不建议靠反射
默认情况下( .GetValue
是pretty的慢)。
This function uses the bubble algorithm to sort a list of IO.DirectoryInfo
by their Name
property.
How I can specify in a parameter the property that I will to sort the list?
For example: "Drive", "Name", "Name.Length", "Directory.Parent", etc...
What I thought like a good idea (maybe is not good, I don't know how much can be improved this) is to pass the parameter as string and then cast the string as...? Here is where I'm lost.
Public Shared Function BubbleSort_List(list As List(Of IO.DirectoryInfo), ByVal SortByProperty As ...) As List(Of IO.DirectoryInfo)
Return list.Select(Function(s) New With { _
Key .OrgStr = s, _
Key .SortStr = System.Text.RegularExpressions.Regex.Replace( _
s.Name, "(\d+)|(\D+)", _
Function(m) m.Value.PadLeft(list.Select(Function(folder) folder.Name.Length).Max, _
If(Char.IsDigit(m.Value(0)), " "c, Char.MaxValue))) _
}).OrderBy(Function(x) x.SortStr).Select(Function(x) x.OrgStr).ToList
End Function
UPDATE:
Notice this part of the code above:
list.Select(Function(folder) folder.Name.Length).Max
What I need is to call the function specifying the property that I want instead "Name" property.
Trying to use the @Sriram Sakthivel solution but it throws an exception at the [property] variable about incompatible casting between UnaryExpression to MemberExpression.
Imports System.Reflection
Imports System.Linq.Expressions
Private Sub Test(sender As Object, e As EventArgs) Handles MyBase.Shown
' Here I create the list
Dim Folders As List(Of IO.DirectoryInfo) = _
IO.Directory.GetDirectories("E:\Música\Canciones", "*", IO.SearchOption.TopDirectoryOnly) _
.Select(Function(p) New IO.DirectoryInfo(p)).ToList()
' Here I try to loop the list at the same time I try to sort it,
' specifying the property I want using @Sriram Sakthivel solution,
' This part does not work because the second parametter is wrong.
For Each folderinfo In BubbleSort_List(Folders, Function() Name)
MsgBox(folderinfo.Name)
Next
End Sub
Private Function BubbleSort_List(list As List(Of IO.DirectoryInfo), exp As Expression(Of Func(Of Object))) As List(Of IO.DirectoryInfo)
Dim [property] As PropertyInfo = DirectCast(DirectCast(exp.Body, MemberExpression).Member, PropertyInfo)
Return list.Select(Function(s) New With { _
Key .OrgStr = s, _
Key .SortStr = System.Text.RegularExpressions.Regex.Replace( _
s.Name, "(\d+)|(\D+)", _
Function(m) m.Value.PadLeft(list.Select(Function(folder) DirectCast([property].GetValue(folder, Nothing), String).Length).Max(), _
If(Char.IsDigit(m.Value(0)), " "c, Char.MaxValue))) _
}).OrderBy(Function(x) x.SortStr).Select(Function(x) x.OrgStr).ToList
End Function
If I understood what you want correctly, the Sriram Sakthivel code sets part of what is required but cannot deliver what you want.
For Each folderinfo In BubbleSort_List(Folders, "Name")
MsgBox(folderinfo.Name)
Next
You have to set a string-type argument with the name of the target property ("Name", "CreationTime", etc.), and retrieve this property from one of the list items (the first one, for example) via GetProperty
; bear in mind that the LINQ query refers to the items, not to the whole list.
Private Function BubbleSort_List(list As List(Of IO.DirectoryInfo), propName As String) As List(Of IO.DirectoryInfo)
Dim curProperty As PropertyInfo = list(0).GetType().GetProperty(propName)
Return list.Select(Function(s) New With { _
Key .OrgStr = s, _
Key .SortStr = System.Text.RegularExpressions.Regex.Replace( _
s.Name, "(\d+)|(\D+)", _
Function(m) m.Value.PadLeft(list.Select(Function(folder) DirectCast(curProperty.GetValue(folder, Nothing), String).Length).Max(), _
If(Char.IsDigit(m.Value(0)), " "c, Char.MaxValue))) _
}).OrderBy(Function(x) x.SortStr).Select(Function(x) x.OrgStr).ToList
End Function
NOTE: I am just proposing a correction of your code to allow you to get what you want as I understood it. I am not recommending to rely on Reflection
by default (.GetValue
is pretty slow).
这篇关于传递IO.DirectoryInfo属性作为参数的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!