C# 错误“不包含适合入口点的静态“主要”方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17095451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:33:52  来源:igfitidea点击:

Error "Does not contain a static "Main" Method suitable for an entry point

c#codedom

提问by GumGun

I got this Error when I try to compile a sourcecode with CodeDom

当我尝试使用 CodeDom 编译源代码时出现此错误

Does not contain a static "Main" Method suitable for an entry point!

不包含适合入口点的静态“Main”方法!

I already googled it and read other answers here, but I dont know how to fix it.

我已经用谷歌搜索了它并在这里阅读了其他答案,但我不知道如何解决它。

Can someone please help me? Here is my source code : http://picz.to/image/ao5n

有人可以帮帮我吗?这是我的源代码:http: //picz.to/image/ao5n

    ^        private void button2_Click(object sender, EventArgs e)
    {
        SaveFileDialog d = new SaveFileDialog();
        d.Filter = "Executable (*.exe)|*.exe";
        if (d.ShowDialog() == DialogResult.OK)
        {
            string source = Properties.Resources.source;
            CompilerParameters param = new CompilerParameters();
            param.CompilerOptions += "/target:winexe" + " " + "/win32icon:" + "\"" + textBox1.Text + "\"";
            param.GenerateExecutable = true;
            param.ReferencedAssemblies.Add("System.Windows.Forms.dll");
            param.ReferencedAssemblies.Add("System.dll");
            param.OutputAssembly = d.FileName;

            StringBuilder Temp = new StringBuilder();
            String InputCode = String.Empty;
            InputCode = "MessageBox.Show((1 + 2 + 3).ToString());";
            Temp.AppendLine(@"using System;");
            Temp.AppendLine(@"using System.Windows.Forms;");
            Temp.AppendLine(@"namespace RunTimeCompiler{");
            Temp.AppendLine(@"static void Main(string[] args){");

            Temp.AppendLine(@"public class Test{");
            Temp.AppendLine(@"public void Ergebnis(){");

            Temp.AppendLine(InputCode);
            Temp.AppendLine(@"}}}}");
            CompilerResults result = new CSharpCodeProvider().CompileAssemblyFromSource(param, Temp.ToString());
            if (result.Errors.Count > 0) foreach (CompilerError err in result.Errors) MessageBox.Show(err.ToString());
            else MessageBox.Show("Done.");
        }
    }

采纳答案by Alexander Matusiak

All C# programs need to contain the Main() method. Essentially this is where the program starts. The code you posted is just a small part of the entire application. You must have removed the location where main had been residing.

所有 C# 程序都需要包含 Main() 方法。基本上这是程序开始的地方。您发布的代码只是整个应用程序的一小部分。您一定已经删除了 main 所在的位置。

MSDN Article on Main

MSDN 上的主要文章

Updated for comments:

更新评论:

A new Windows Form App has a Program class that instantiates the form that you want.

新的 Windows 窗体应用程序有一个 Program 类,用于实例化所需的窗体。

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
     }

Try copying that over to a new file called program.cs. Make sure that Form1 now points to the form you created in the applications.

尝试将其复制到名为 program.cs 的新文件中。确保 Form1 现在指向您在应用程序中创建的表单。

回答by Cortright

Paste this into your class -- if you still get an error, you need to paste the entire class in question, not just a screen capture of the event handler for a button click.

将其粘贴到您的类中——如果仍然出现错误,则需要粘贴整个类,而不仅仅是按钮单击的事件处理程序的屏幕截图。

static void Main(string[] args)
{
  //do nothing
}

回答by Jonesopolis

The code you've posted is the click event for a button. A button is usually on a form, and the form must be initialized. If you create a Windows Forms Application it will create a file Program.cs that contains a Main method that starts your form.

您发布的代码是按钮的点击事件。按钮通常位于窗体上,并且必须对窗体进行初始化。如果您创建 Windows 窗体应用程序,它将创建一个文件 Program.cs,其中包含启动窗体的 Main 方法。

When you start a program, the computer needs to know where to actually start running code, that's what the Main() method is for. It is required to run, and that's the error you are receiving.

当你启动一个程序时,计算机需要知道从哪里真正开始运行代码,这就是 Main() 方法的用途。它需要运行,这就是您收到的错误。