CSharpCodeProvider sounds like it can do the trick. However, before using this, I would ask 2 questions: is it required to be strictly the C # string literal syntax and is it a trusted input file?
CSharpCodeProvider obviously provides exactly the C # compiler syntax, but it seems to me that for this it would be relatively easy to inject some code into your process through this route.
Javascript string literal syntax is pretty close to C # string literal syntax, and .NET includes a JavaScriptSerializer class that can parse such string literals without injecting them into the code in the current process.
source share