Pass an IO.DirectoryInfo property as a parameter to a function?

故事扮演 提交于 2019-12-04 19:37:38

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).

Upto single level of properties you can do with MemberExpression. obj.Prop.Prop2 requires use of UnaryExpression

Private Shared Sub DoSomething(list As List(Of DirectoryInfo), exp As Expression(Of Func(Of Object)))
    Dim member As MemberExpression

    If (TypeOf exp.Body Is UnaryExpression) Then
        member = DirectCast(DirectCast(exp.Body, UnaryExpression).Operand, MemberExpression)
    Else
        member = DirectCast(exp.Body, MemberExpression)
    End If

    Dim [property] As PropertyInfo = DirectCast(member.Member, PropertyInfo)

'You could then use it like
list.Select(Function(folder) DirectCast([property].GetValue(folder, Nothing), String).Length).Max()
End Sub

Private Shared Sub Main()
Dim dir = New DirectoryInfo("somedirectory")
DoSomething(list, Function() dir.Parent)
    DoSomething(list, Function() dir.Name)
    DoSomething(list, Function() dir.FullName)

    DoSomething(list, Function() dir.Parent.Name)'Requires additional effort
End Sub

May be syntax error. am basically c# programmer. I just used converter tool for Vb.net

Edit:

Since you have list of directories you have a doubt how to pass dir.Name parameter doesn't matter actually, dir.Name is just passed to capture PropertyInfo of it.

So you can simply pass New DirectoryInfo("somedirectory").Name. Try the following

Dim dir = New DirectoryInfo("SomeArbitaryStringIsEnough")

For Each folderinfo In BubbleSort_List(Folders, Function() dir.Name)
    MsgBox(folderinfo.Name)
Next
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!