找不到合适的主方法

本文关键字:方法 找不到 | 更新日期: 2023-09-27 18:37:17

我正在尝试创建一个Windows表单应用程序,它可以创建另一个Windows表单应用程序。但是当我尝试在 c# 代码中使用 CodeDom 进行编译时,我遇到的错误很奇怪。

'kjpUnityGameLauncherTemplate.RunLauncher' does not have a suitable static Main method

这让我有点困惑,因为类"RunLauncher"确实有一个 main 方法,默认设置在 (http://msdn.microsoft.com/) 站点中描述。

RunLauncher 类:http://pastebin.com/NU3VYwpv(具有 main 方法)

我用来实际编译此代码的代码。CodeDom是这样的:

if (codeProvider.Supports(GeneratorSupport.EntryPointMethod))
{
    parameters.MainClass = "kjpUnityGameLauncherTemplate.RunLauncher";
}
CodeCompileUnit compileUnits = new CodeCompileUnit();
CodeNamespace nsp = new CodeNamespace("kjpUnityGameLauncherTemplate");
parameters.CompilerOptions = "/main:kjpUnityGameLauncherTemplate.RunLauncher";
CodeTypeDeclaration class1 = new CodeTypeDeclaration("RunLauncher");
nsp.Types.Add(class1);
CodeTypeDeclaration class2 = new CodeTypeDeclaration("kjpUnityGameLauncher");
nsp.Types.Add(class2);
CodeTypeDeclaration class3 = new CodeTypeDeclaration("Launcher");
nsp.Types.Add(class3);
nsp.Imports.Add(new CodeNamespaceImport("kjpUnityGameLauncherTemplate"));
compileUnits.Namespaces.Add(nsp);
CompilerResults results = icc.CompileAssemblyFromDom(parameters, compileUnits);

还有一些其他的东西,比如变量"codeProvider"的声明等,但在这种情况下,这些都不是问题,这就是为什么我没有包括它们。

找不到合适的主方法

创建可执行文件 您的代码必须声明并正确设置入口点方法才能在 CodeDom 中运行。我没有看到在上面的示例中声明了一个。下面我有一个来自MSDN的示例,位于

http://msdn.microsoft.com/en-us/library/y2k85ax6.aspx

CodeEntryPointMethod start = new CodeEntryPointMethod();
CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression(
new CodeTypeReferenceExpression("System.Console"),
"WriteLine", new CodePrimitiveExpression("Hello World!"));
start.Statements.Add(cs1);