4

I'm searching for a function (perhaps there is one in the .Net-Framework) which I can use to validate C#-syntax. It can also be a c# parser with build-in validation.

Background: I'm building a little Code-Generator where the user can enter some definitions and gets back full implemented properties etc., but I want to check if the user entered correct C# and if the result is correct, too.

Edit:

I don't want to compile the code, and it could be uncomplete code. So that for example the User could enter a code-snippet with classes from his code, I don't know, and I only want to validate the syntax, not the 'content'

Examples

Input:

Car car = new Car();
car.drive("50");

Output:

Same as input, because it's valid and nothing to do.

Input:

Car car = new Car()
car.drive("50");

Output:

Message that it is not valid, beause of missing ';'

etc.

I don't know the class Car and therefore I can't compile it, I only want to check the syntax.

10
  • C# already has a compiler for itself built-in, which will necessarily validate code that it compiles Commented Aug 9, 2011 at 15:08
  • Yeah, I know that, but I want to check the syntax from a running c#-program (Code is just in a TextBox), and it won't be compiled by my program Commented Aug 9, 2011 at 15:09
  • What's the disadvantage to quickly trying to compile the code? There's no other 'real' way to determine if it's completely valid. Unless you try to compile it, you won't know Commented Aug 9, 2011 at 15:10
  • @Kieren see my last edit, I can't compile because I don't know the full Code... Commented Aug 9, 2011 at 15:25
  • 1
    If you don't know the full code, then how can you possibly know if it's valid or not in advance? (You can't) Without knowing whether Car is a type, namespace, variable identifier, type parameter etc, you can't validate the syntax fully. The snippet might be valid or invalid (syntax-wise). What then do you mean by 'validate'? Commented Aug 9, 2011 at 15:31

3 Answers 3

4

How to programatically compile code using the C# compiler:

http://support.microsoft.com/kb/304655

..it validates as a part of the compilation process. You can get the list of errors like this (from the article..surprising since it's a bit messy):

foreach(CompilerError CompErr in results.Errors)
{
 textBox2.Text = textBox2.Text +
     "Line number " + CompErr.Line + 
     ", Error Number: " + CompErr.ErrorNumber + 
     ", '" + CompErr.ErrorText + ";" + 
     Environment.NewLine + Environment.NewLine;
}
Sign up to request clarification or add additional context in comments.

Comments

2

Have a look at this .Net class, http://msdn.microsoft.com/en-us/library/system.codedom.compiler.codedomprovider.aspx

Basically you can call the compiler on the code your looking at and receive back any error messages generated.

Edit: If the code may not compile thats OK, you can look at the error messages returned and ignore stuff like the 'not defined' errors.

You're only other option I think it to write a RegEx that matches the C# language spec.

1 Comment

Just a comment, such a RegEx I would expect to be several kilobytes long, and not validate fully/correctly :)
1

You'll have to cookup some compilable C# code (using CodeDom or just adding manual snippets around what your users will manually code), and then compile it, as described here: How to programmatically compile code using C# compiler

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.