当我尝试使用CodeDom编译源代码时出现此错误
不包含适用于入口点的静态"主"方法!
我已经在Google上搜索并阅读了其他答案,但我不知道如何修复它.
有人可以帮帮我吗?这是我的源代码: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.");
}
}
Run Code Online (Sandbox Code Playgroud)
所有C#程序都需要包含Main()方法.基本上这是程序启动的地方.您发布的代码只是整个应用程序的一小部分.您必须删除main所在的位置.
更新评论:
新的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());
}
}
Run Code Online (Sandbox Code Playgroud)
尝试将其复制到名为program.cs的新文件中.确保Form1现在指向您在应用程序中创建的表单.