Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 是否将IO.DirectoryInfo属性作为参数传递给函数?(第二部分)_.net_Vb.net_Linq_Reflection_Directoryinfo - Fatal编程技术网

.net 是否将IO.DirectoryInfo属性作为参数传递给函数?(第二部分)

.net 是否将IO.DirectoryInfo属性作为参数传递给函数?(第二部分),.net,vb.net,linq,reflection,directoryinfo,.net,Vb.net,Linq,Reflection,Directoryinfo,在另一个问题(此处:)中,我询问了如何改进函数以将DirectoryInfo属性作为参数传递,问题是代码仅适用于“顶级”属性,如“名称”、“根”、“驱动器”等 但我需要使用如下函数: Dim Folders As List(Of IO.DirectoryInfo) = blah bla blah... For Each folderinfo In Bubble_Sort_List_DirectoryInfo(Folders, Function() New IO.DirectoryInfo(""

在另一个问题(此处:)中,我询问了如何改进函数以将DirectoryInfo属性作为参数传递,问题是代码仅适用于“顶级”属性,如“名称”、“根”、“驱动器”等

但我需要使用如下函数:

Dim Folders As List(Of IO.DirectoryInfo) = blah bla blah...

For Each folderinfo In Bubble_Sort_List_DirectoryInfo(Folders, Function() New IO.DirectoryInfo("").Name)
    MsgBox(folderinfo.Name)
Next
2000-2006
2007
2008
80's
90's
但我需要像这样使用函数:

For Each folderinfo In Bubble_Sort_List_DirectoryInfo(Folders, Function() New IO.DirectoryInfo("").Parent.Name.Length)
    MsgBox(folderinfo.Name)
Next
要管理DirectoryInfo属性(如“Name.Length”或“Parent.Name.Length”)的使用,需要在此函数中添加/修改哪些内容

更新:

这只是一些解释和例子

在驱动器的目录中,我有一些文件夹,其文件夹名称如下:

80's
90's
2000-2006
2007
2008
在我的应用程序中,我使用“IO.Directory.GetDirectories”方法获取文件夹,并将它们存储到DirectoryInfo()列表中

这是输入列表:

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()
…但“IO”方法会导致列表内容按字符串排序,如下所示:

Dim Folders As List(Of IO.DirectoryInfo) = blah bla blah...

For Each folderinfo In Bubble_Sort_List_DirectoryInfo(Folders, Function() New IO.DirectoryInfo("").Name)
    MsgBox(folderinfo.Name)
Next
2000-2006
2007
2008
80's
90's
我想要的输出是:

80's
90's
2000-2006
2007
2008
因此,在使用“IO”方法创建列表后,我需要对列表内容进行排序,以获得所需的输出,这正是我使用上述函数所得到的调用函数,使用属性“Name”作为参数,按文件夹的Name属性对文件夹进行气泡排序,从而获得所需的输出


问题是我需要使用其他属性,如“Name.Length”和“Parent.Name.Length”,但该函数只允许使用“Name”、“Parent”等“TopLevel”属性,而不允许使用变量属性。

创建一个实现IComparer的类:

Public Class MyDirectoryInfoComparer
    Implements IComparer(Of IO.DirectoryInfo)

    Public Function Compare(x As IO.DirectoryInfo, y As IO.DirectoryInfo) As Integer _
        Implements IComparer(Of IO.DirectoryInfo).Compare

        ' x comes before y
        Return -1 ' or any number less than 0

        ' x is the same as y
        Return 0

        ' x comes after y
        Return 1 ' or any number greater than 0
End Function
创建您的列表:

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()
然后使用比较器类对其进行排序:

Folders.Sort(New MyDirectoryInfoComparer)

创建一个实现IComparer的类:

Public Class MyDirectoryInfoComparer
    Implements IComparer(Of IO.DirectoryInfo)

    Public Function Compare(x As IO.DirectoryInfo, y As IO.DirectoryInfo) As Integer _
        Implements IComparer(Of IO.DirectoryInfo).Compare

        ' x comes before y
        Return -1 ' or any number less than 0

        ' x is the same as y
        Return 0

        ' x comes after y
        Return 1 ' or any number greater than 0
End Function
创建您的列表:

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()
然后使用比较器类对其进行排序:

Folders.Sort(New MyDirectoryInfoComparer)

我还没有看过第1部分,但是为什么不传递一个lambda表达式而不是字符串呢?因此,您可以使用一些函数(x)x.parent.name.length而不是“parent.name.length”,然后通过addressof或作为变量传递它(因为简单)。@neolik感谢您的评论,一个人向我展示了一个使用lambda的解决方案,但也有同样的问题,我更新了问题的第一部分的链接。我优先使用字符串,因为在这种情况下,lambda目录路径可能为空(例如:function()new directoryinfo(“”.Name),但我希望以通用方式使用此函数,以便在其他类型的对象中,lambda表达式将迫使我在无法执行时执行对象实例,我希望您能理解我,也许我对lambda的用法有很多误解,但无论如何,使用string或lambda我无法解决这个问题,您不需要实例化对象。可以在尚未存在的对象上创建lambda。把它看作一个纯函数。纯函数不需要任何东西,它们定义了工作流。有些东西进了,有些东西出了,什么也没有存储。若您想要泛型方法,可以对类型对象执行lambda。没有运行时检查,与字符串基本相同,但您保留了“很好”的lambda语法。@Neolisk谢谢您的解释,那么我不能使用lambdas,有任何风险:)。我已经使用lambda版本更新了代码,请帮助我!我还没有看过第1部分,但是为什么不传递一个lambda表达式而不是字符串呢?因此,您可以使用一些函数(x)x.parent.name.length而不是“parent.name.length”,然后通过addressof或作为变量传递它(因为简单)。@neolik感谢您的评论,一个人向我展示了一个使用lambda的解决方案,但也有同样的问题,我更新了问题的第一部分的链接。我优先使用字符串,因为在这种情况下,lambda目录路径可能为空(例如:function()new directoryinfo(“”.Name),但我希望以通用方式使用此函数,以便在其他类型的对象中,lambda表达式将迫使我在无法执行时执行对象实例,我希望您能理解我,也许我对lambda的用法有很多误解,但无论如何,使用string或lambda我无法解决这个问题,您不需要实例化对象。可以在尚未存在的对象上创建lambda。把它看作一个纯函数。纯函数不需要任何东西,它们定义了工作流。有些东西进了,有些东西出了,什么也没有存储。若您想要泛型方法,可以对类型对象执行lambda。没有运行时检查,与字符串基本相同,但您保留了“很好”的lambda语法。@Neolisk谢谢您的解释,那么我不能使用lambdas,有任何风险:)。我已经使用lambda版本更新了代码,请帮助我!