如何利用字符串的自动完成?

时间:2014-12-23 21:20:45

标签: vb.net string visual-studio list visual-studio-2013

我正在编写一个应用程序,我必须将字符串作为参数传递。像这样:

GetValue("InternetGatewayDevice.DeviceInfo.Description")
GetValue("InternetGatewayDevice.DeviceInfo.HardwareVersion")
CheckValue("InternetGatewayDevice.DeviceInfo.Manufacturer")
ScrambleValue("InternetGatewayDevice.DeviceInfo.ModelName")
DeleteValue("InternetGatewayDevice.DeviceInfo.ProcessStatus.Process.1")

完整列表大约有10500个条目,我认为如果我拼错了,我真的会迷失方向。

所以我试图为每个字符串段声明一个名称空间(由"。"分隔),并将last声明为一个简单的类,扩展为其FullName的String(基本应用程序名称空间除外) ):

Class xconv
    Public Shared Widening Operator CType(ByVal d As xconv) As String

        Dim a As String = d.GetType.FullName
        Dim b As New List(Of String)(Strings.Split(a, "."))
        Dim c As String = Strings.Join(b.Skip(1).ToArray, ".")

        Return c

    End Operator
End Class

所以我有这些声明:

Namespace InternetGatewayDevice
        Namespace DeviceInfo
                Class Description
                    Inherits xconv
                End Class
        End Namespace
End Namespace

这样,IntelliSense非常乐意为我自动填充该字符串。

现在我必须为每个可能的字符串执行此操作,因此我选择(为了保持理智)制作一个方法来执行此操作:

    Sub Create_Autocomlete_List()

        Dim pathlist As New List(Of String)(IO.File.ReadAllLines("D:\list.txt"))
        Dim def_list As New List(Of String)
        Dim thedoc As String = ""

        For Each kl As String In pathlist
            Dim locdoc As String = ""
            Dim el() As String = Strings.Split(kl, ".")
            Dim elc As Integer = el.Length - 1
            Dim elz As Integer = -1
            Dim cdoc As String
            For Each ol As String In el
                elz += 1

                If elz = elc Then
                    locdoc += "Class " + ol + vbCrLf + _
                        "Inherits xconv" + vbCrLf + _
                        "End Class"
                Else
                    locdoc += "Namespace " + ol + vbCrLf
                    cdoc += vbCrLf + "End Namespace"
                End If

            Next

            locdoc += cdoc
            thedoc += locdoc + vbCrLf + vbCrLf

        Next

        IO.File.WriteAllText("D:\start_list_dot_net.txt", thedoc)

    End Sub

真正的问题是,这是可怕的缓慢和内存密集(现在我点了一个OutOfMemory异常),我不知道Intellisense如何使用Create_Autocomlete_List的(在不久的将来不可用)输出执行( )sub。

我相信它会很慢。

所以真正的问题是:我这样做了吗?有没有更好的方法将字符串列表映射到自动完成字符串?有没有"标准"这样做的方法?

在这种情况下你会做什么?

1 个答案:

答案 0 :(得分:0)

我不知道Visual Studio将如何在数千个类中执行,但您可以优化Create_Autocomlete_List方法,以便在构建源代码时不将所有内容存储在内存中以最大限度地减少内存使用量。这也应该大大加快速度。

它也可以简化,因为嵌套的命名空间可以在一行上声明,例如Namespace First.Second.Third

Sub Create_Autocomlete_List()

    Using output As StreamWriter = IO.File.CreateText("D:\start_list_dot_net.txt")
        For Each line As String In IO.File.ReadLines("D:\list.txt")
            Dim lastDotPos As Integer = line.LastIndexOf("."c)
            Dim nsName As String = line.Substring(0, lastDotPos)
            Dim clsName As String = line.Substring(lastDotPos + 1)
            output.Write("Namespace ")
            output.WriteLine(nsName)
            output.Write("    Class ")
            output.WriteLine(clsName)
            output.WriteLine("        Inherits xconv")
            output.WriteLine("    End Class")
            output.WriteLine("End Namespace")
            output.WriteLine()
        Next
    End Using

End Sub

请注意使用File.ReadLines而不是File.ReadAllLines,它会返回IEnumerable而不是数组。另请注意,输出直接写入文件,而不是内置在内存中。

注意根据您的示例数据,您可能会遇到最后一个节点不是有效类名的问题。例如InternetGatewayDevice.DeviceInfo.ProcessStatus.Process.1 - 1不是VB.NET中的有效类名。您将需要设计一些机制来处理这个问题 - 也许您可以在扩展运算符中删除一些唯一的前缀。

我也不确定结果类的可用性,因为大概你需要将一个实例传递给方法:

GetValue(New InternetGatewayDevice.DeviceInfo.Description())

在类上使用Shared字符串似乎更好:

Namespace InternetGatewayDevice
    Class DeviceInfo
        Public Shared Description As String = "Description"
        Public Shared HardwareVersion As String = "HardwareVersion"
        ' etc.
    End Class
End Namespace

所以你可以引用这些字符串:

GetValue(InternetGatewayDevice.DeviceInfo.Description)

但是,我认为由于嵌套的级别不同而没有创建名称冲突会更难以生成。