不包含静态';Main';适合作为切入点的方法;
本文关键字:切入点 方法 Main 包含 静态 | 更新日期: 2023-09-27 18:20:39
我环顾四周,没有人能回答这个问题。我有一个静态的主空隙,看起来应该可以工作。
编译器:
CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
string Output = "Out.exe";
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, Properties.Resources.source);
if (results.Errors.Count > 0)
{
foreach (CompilerError CompErr in results.Errors)
{
WinBody.Text =
"Line number " + CompErr.Line +
", Error Number: " + CompErr.ErrorNumber +
", '" + CompErr.ErrorText + ";" +
Environment.NewLine + Environment.NewLine;
}
}
else
{
//Successful Compile
MessageBox.Show("yay");
}
source.txt
using System;
namespace HelloWorld
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class HelloWorldClass
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.ReadLine();
}
}
为什么我在这方面出错了?我真的不明白。同样的代码也适用于我的其他项目。
您的代码没有问题。。我试过了。如果你用你提供的实际代码替换你的Properties.Resources.source
,你会注意到有一个编译器错误。因此,问题在于你的资源。仔细检查一下。
也就是说,有一个名为MainClass
的属性可以应用于CompilerParameters
。这可以让你选择你的入口点在哪里。
MSDN:http://msdn.microsoft.com/en-us/library/system.codedom.compiler.compilerparameters.mainclass(v=vs.110).aspx
错误在于必须设置命名空间的右大括号。你的来源必须看起来像:
using System;
namespace HelloWorld
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class HelloWorldClass
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.ReadLine();
}
}
}
你能检查所有的开放圆括号都是封闭的吗?我可以看到命名空间末尾缺少一个。。。还要检查是否在其他类中声明了任何其他Main()类。。。
您的source.txt文件不正确(不确定它是否被错误地粘贴到堆栈溢出中)。应为:
using System;
namespace HelloWorld
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class HelloWorldClass
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.ReadLine();
}
}
// Add missing bracket
}
您的编译器代码片段不包括实际的主条目函数,但我在自己的应用程序中按原样尝试了您的代码。我创建了一个WPF C#应用程序(WpfApplication2),添加了source.txt
,并在主窗口中添加了一个WinBody
文本框,并使用了以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.CodeDom.Compiler;
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
string Output = "Out.exe";
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, Properties.Resources.source);
if (results.Errors.Count > 0)
{
foreach (CompilerError CompErr in results.Errors)
{
WinBody.Text +=
"Line number " + CompErr.Line +
", Error Number: " + CompErr.ErrorNumber +
", '" + CompErr.ErrorText + ";" +
Environment.NewLine + Environment.NewLine;
}
}
else
{
//Successful Compile
MessageBox.Show("yay");
//Run App
Process.Start(Output);
}
}
}
}
InitializeComponent()
之后的MainWindow()
中的代码几乎是逐字逐句地取自OP的代码片段。将WinBody.Text =
的异常更改为WinBody.Text +=
,以便连接多个错误消息;最后,我使用Process.Start(Output);
运行编译后的应用程序,以验证它是否打印Hello World!
。成功运行并打印Hello World!
。如果我使用缺少括号的OP的source.txt,我不会得到"yay"(这是正确的),并且Textbox有这样的错误:"行编号15,错误编号:CS1513,'}应为;"这是人们所期望的。