Constant error in compiler using C#'s provided objects

Posted by dotnetdev on Stack Overflow See other posts from Stack Overflow or by dotnetdev
Published on 2009-05-23T21:46:18Z Indexed on 2010/03/26 21:03 UTC
Read the original article Hit count: 403

Filed under:

I have used the built in C# methods to write a compiler, like the following:

    CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
    string Output = "Out.exe";
    Button ButtonObject = (Button)sender;

   this.RadTextBox1.Text = string.Empty;

    System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();

    //Make sure we generate an EXE, not a DLL
    parameters.GenerateExecutable = true;
    parameters.OutputAssembly = Output;

    CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, RadTextBox1.Text);

    if (results.Errors.Count > 0)
    {
        RadTextBox2.ForeColor = Color.Red;
        foreach (CompilerError CompErr in results.Errors)
        {
            RadTextBox2.Text = RadTextBox2.Text +
                        "Line number " + CompErr.Line +
                        ", Error Number: " + CompErr.ErrorNumber +
                        ", '" + CompErr.ErrorText + ";" +
                        Environment.NewLine + Environment.NewLine;
        }
    }

    else
    {
        //Successful Compile
        RadTextBox2.ForeColor = Color.Blue;

        Guid guid =            Guid.NewGuid();

        string PathToExe = Server.MapPath(Path.Combine(@"\Compiled" , Output));

        FileStream fs = System.IO.File.Create(PathToExe);

        using (StreamWriter sw = new StreamWriter(fs))
        {

            sw.Write(RadTextBox1.Text);

        }



        Response.WriteFile(PathToExe);

When I run this code and write a Main method (such as the code sample in http://msdn.microsoft.com/en-us/library/ms228506(VS.80).aspx, I get this error:

Line number 0, Error Number: CS5001, 'Program 'c:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\Out.exe' does not contain a static 'Main' method suitable for an entry point;

The code above is used as the basis of a compiler on my site (not yet live). So you type in code and generate an .exe assembly. But when I enter code into the textbox for code writing (Radtextbox1), even with a main method, I get the error.

What gives?

Thanks

© Stack Overflow or respective owner

Related posts about c#