Programmatically Compiling with CSharpCodeProvider Error: "Too many characters in character literal" RRS feed

  • Question

  • My problem is that I have a textbox. I enter a sentence into the textbox. When I click the enter button, it runs this code. The string variable 'code' is where my writeline with the sentence I entered is. My header is all my using statements and the footer is the ending }'s. Whenever I enter any text, this error message pops up: "Too many characters in character literal". How do I fix this?

    This is my code:

                    CSharpCodeProvider cse = new CSharpCodeProvider
        (new Dictionary<string, string>() { {
                            "CompilerVersion", "v3.5" } });
                    CompilerParameters par = new CompilerParameters
                        (new[] { "mscorlib.dll", "System.Core.dll" }, name + ".exe", true);
                    par.GenerateExecutable = true;
                    par.GenerateInMemory = true;
                    CompilerResults results = cse.CompileAssemblyFromSource(par,
                      header + code + footer);
                    results.Errors.Cast<CompilerError>().ToList().ForEach(error => MessageBox.Show("FATAL ERROR: " + error.ErrorText.ToUpper()));

    Monday, February 19, 2018 6:06 PM

All replies

  • Probably the text resulted from header + code + footer does not represent a valid program.

    Show the value of header + code + footer.

    Monday, February 19, 2018 7:18 PM
  • Do you mean you want me to show you what is in all of them?? Please explain more.

    Monday, February 19, 2018 8:10 PM
  • You need to examine and show us what is in your variables of header + code + footer.

    Evidently, the contents of these strings are not a valid program. So when you try to compile it with CompileAssemblyFromSource you are getting the error.

    Tuesday, February 20, 2018 7:52 AM