using System; using Microsoft.CSharp; using System.Collections.Generic; using System.CodeDom.Compiler; using System.Diagnostics; class Program { static void Main(string[] args) { string sourceCode = "class e{static void Main(){System.Console.Write(new char[1\n#if l\n#else\n*5]{'w','o','r','l','d'\n#if o\n#else\n});\n#endif\n}\n#endif\n}"; Console.WriteLine(isPristine(sourceCode) ? "Pristine" : "Not pristine"); Console.ReadLine(); } static bool isPristine(string sourceCode) { using (var csc = new CSharpCodeProvider(new Dictionary() { { "CompilerVersion", "v3.5" } })) { var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }); int length = sourceCode.Length; List substringsThatWork = new List(); for (int i = 0; i < length; i++) { for (int j = 1; j < length - i; j++) { string sourceWithRemoval = sourceCode.Remove(i, j); CompilerResults results = csc.CompileAssemblyFromSource(parameters, sourceWithRemoval); if (results.Errors.Count == 0) { Debug.WriteLine("Still works by removing:"); Debug.WriteLine("@{0}@ ", sourceWithRemoval); Debug.WriteLine("At index {0}.", i); return false; } } } } return true; } }