Valid property name checker
I am using vb.net 2010, trying to generate simple classes with properties using T4 from database. Sometimes I get an error that some string is not a valid property name because it is a vb keyword. For example based on database data, my T4 tried to create a cla开发者_如何学Goss with a property called "property".
Is there a function that check whether a string is a keyword, like:
dim a = "property"
if iskeyword(a) then
a &= "1"
end if
Keywords can be made into valid identifiers in VB by putting them into […]
. You could do that for every identifier here, to prevent name clashes. For example the following code compiles:
Dim [Dim] As [As] = New [New]()
given
Class [As]
End Class
Class [New] : Inherits [As]
End Class
Based on everything I have ever read, keywords are part of the compiler and there is no method you can use to check them built in as they are not exposed.
It would appear in this case you are stuck with using a known list of keywords to build your own check.
Just like @D said, looks like I have to create it myself:
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
精彩评论