C# method contents validation

c#, validation

Solution

You'll need to scope your problem more carefully in order to get a sensible answer.

For example, what are you going to do about methods that contain preprocessor directives?

void M()
{

#if FOO
    for(foo;bar;blah) {
#else
    while(abc) {
#endif
        Blah();
    }
}

This is silly but legal, so you have to handle it. Are you going to count that as a mismatched brace or not?

Can you provide a detailed specification of exactly what you want to determine? As we've seen several times on this site, people cannot successfully build a routine that divides two numbers without a specification. You're talking about analysis that is far more complex than dividing two numbers; the code which does what you're describing in the actual compiler is tens of thousands of lines long.

Problem

I need to validate the contents of a C# method. I do not care about syntax errors that do not affect the method's scope. I do care about characters that will invalidate parsing of the rest of the code. For example: ``` method() { /* valid comment */ /* <-- bad for (i..) { } for (i..) { <-- bad } ``` I need to validate/fix any non-paired characters. This includeds /* */, { }, and maybe others. How should I go about this? My first thought was Regex, but that clearly isn't going to get the job done.

Original source