"Daniel O'Connell [C# MVP]" wrote:
"Wavemaker" wrote:
I was wondering if there is way in the .NET framework to test if a
string represents a valid identifier name for the C# language. I want
to write a program that will generate code based on user input. Part
of the program's job will be to use names provided by the user to name
classes, methods, etc. Some error checking would be involved to make
sure the names use legal C# syntax. Before rolling my own code to do
the error checking, I wanted to check to see if this functionality
already exists.
If you are using the code dom, I'm pretty sure this is handled for
you(it'll output identifiers named the same as keywords with the @
prefix).
Otheriwse, if you are generating code by hand, you will have to write
code to do it yourself, I'm afraid.
It's the latter. I've been thinking about this, though, and it shouldn't
be too hard to do this myself. The trickest part seems to be the first
character in the name. It will have to be treated as a special case.
Here's a small algorithm I just wrote for testing class names:
- Trim all whitespace from the beginning and end of the string.
- If the first character is NOT an underscore OR NOT a character OR NOT
an asterick, the identifier is NOT valid.
Return false.
- For each remaining character:
- If the character is NOT an underscore OR NOT a number OR NOT a
character, the identifier is NOT valid.
Return false.
- Return true.
I think this algorithm should work for what I'm doing, unless I've
missed something. Any comments will be welcomed.