【问题标题】:Valid property name checker有效的属性名称检查器
【发布时间】:2011-06-17 21:21:00
【问题描述】:

我正在使用 vb.net 2010,尝试使用数据库中的 T4 生成具有属性的简单类。有时我会收到一个错误,即某些字符串不是有效的属性名称,因为它是 vb 关键字。例如基于数据库数据,我的 T4 尝试创建一个具有名为“property”的属性的类。

是否有检查字符串是否为关键字的函数,如:

dim a = "property"
if iskeyword(a) then
    a &= "1"
end if

【问题讨论】:

    标签: vb.net t4


    【解决方案1】:

    通过将关键字放入[…],可以将关键字变成有效的标识符。您可以对此处的每个标识符执行此操作,以防止名称冲突。例如下面的代码编译:

    Dim [Dim] As [As] = New [New]()
    

    给定

    Class [As]
    End Class
    
    Class [New] : Inherits [As]
    End Class
    

    【讨论】:

    • 酷!点赞!虽然为了避免混淆,我不会在我的代码中使用它。
    • @Endy 你是对的,你不应该。此外,我真的希望您找到原始问题的“真实”答案。这更像是最后的解决方案。
    【解决方案2】:

    根据我所读过的所有内容,关键字是编译器的一部分,没有任何方法可以用来检查内置的它们,因为它们没有暴露出来。

    在这种情况下,您可能会被困在使用已知的关键字列表来构建自己的检查。

    【讨论】:

    • 看来你是对的,但我在等,也许有人有什么诡计。
    • 那会很方便。大约一年前,我自己也遇到过这个问题。
    【解决方案3】:

    就像@D说的,看来我得自己创造了:

    Function IsKeyWord(name As String) As Boolean
        Dim keywords As New List(Of String) From {
            "addhandler", "addressof", "alias", "and", "andalso", "as",
            "boolean", "byref", "byte", "byval", "call", "case", "catch", "cbool",
            "cbyte", "cchar", "cdate", "cdec", "cdbl", "char", "cint", "class",
            "clng", "cobj", "const", "continue", "csbyte", "cshort", "csng",
            "cstr", "ctype", "cuint", "culng", "cushort", "date", "decimal",
            "declare", "default", "delegate", "dim", "directcast", "do", "double",
            "each", "else", "elseif", "end", "endif", "enum", "erase", "error",
            "event", "exit", "false", "finally", "for", "friend", "function",
            "get", "gettype", "getxmlnamespace", "global", "gosub", "goto",
            "handles", "if", "implements", "imports", "in", "inherits", "integer",
            "interface", "is", "isnot", "let", "lib", "like", "long", "loop", "me",
            "mod", "module", "mustinherit", "mustoverride", "mybase", "myclass",
            "namespace", "narrowing", "new", "next", "not", "nothing",
            "notinheritable", "notoverridable", "object", "of", "on", "operator",
            "option", "optional", "or", "orelse", "overloads", "overridable",
            "overrides", "paramarray", "partial", "private", "property",
            "protected", "public", "raiseevent", "readonly", "redim", "rem",
            "removehandler", "resume", "return", "sbyte", "select", "set",
            "shadows", "shared", "short", "single", "static", "step", "stop",
            "string", "structure", "sub", "synclock", "then", "throw", "to",
            "true", "try", "trycast", "typeof", "variant", "wend", "uinteger",
            "ulong", "ushort", "using", "when", "while", "widening", "with",
            "withevents", "writeonly", "xor"}
        Return keywords.Contains(name.Trim.ToLower)
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-25
      • 2012-05-09
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      相关资源
      最近更新 更多