在一个类中,我编写了一些方法来检索一些文件,现在为了简化代码编写,我编写了这个私有函数,它需要一个Type:
Private Shared Function GetDeletedItems(Of T)(Optional ByVal DriveLetter As Char = Nothing) As T()
Return (From Item As ShellObject In RecycleBin
Where Item.GetType = GetType(T) _
AndAlso If(DriveLetter = Nothing,
Nothing,
Item.Name.StartsWith(DriveLetter,
StringComparison.InvariantCultureIgnoreCase))).
Cast(Of T).
ToArray()
End Function
然后只应该从类中调用这个公共函数,我尝试设置正确的Type来调用私有函数并返回数据:
Public Shared Function GetDeletedItems(ByVal ItemType As ShellItemType,
Optional ByVal DriveLetter As Char = Nothing)
Dim CurrentType As Type = Nothing
Select Case ItemType
Case ShellItemType.All
CurrentType = GetType(ShellObject)
Case ShellItemType.Files
CurrentType = GetType(ShellFile)
Case ShellItemType.Folders
CurrentType = GetType(ShellFolder)
End Select
Return GetDeletedItems(Of CurrentType)(DriveLetter)
End Function
问题是我在Return指令上遇到错误'因为它说CurrentType
没有定义,我做错了什么?
答案 0 :(得分:1)
首先,您不需要变量CurrentType
,编译器将不乐意编译它。摆脱它。
这是否符合您的要求?
Public Shared Function GetDeletedItems(ByVal ItemType As ShellItemType,
Optional ByVal DriveLetter As Char = Nothing)
Select Case ItemType
Case ShellItemType.All
Return GetDeletedItems(Of ShellObject)(DriveLetter)
Case ShellItemType.Files
Return GetDeletedItems(Of ShellFile)(DriveLetter)
Case ShellItemType.Folders
Return GetDeletedItems(Of ShellFolder)(DriveLetter)
End Select
End Function
答案 1 :(得分:1)
查看你的代码我没看到分成两个函数的原因。我认为你的外部函数应该是通用函数,你应该完全杀掉ShellItemType。
该方法只是你的内部私有方法,只有你公开:
Public Shared Function GetDeletedItems(Of T)(Optional ByVal DriveLetter As Char = Nothing) As T()
Return (From Item As ShellObject In RecycleBin
Where Item.GetType = GetType(T) _
AndAlso If(DriveLetter = Nothing,
Nothing,
Item.Name.StartsWith(DriveLetter,
StringComparison.InvariantCultureIgnoreCase))).
Cast(Of T).
ToArray()
End Function
然后你会这样称呼它:
Dim results = GetDeletedItems(Of ShellObject)(driveLetter)
看来您正在根据类型在方法之外设置ShellItemType变量,因此您实际上可以自己发送该类型。