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.
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):