我正在用 C# 编写一种连接语言,目前它是解释型的,但我想进行下一步:编译。为此,我尝试编写一个简单的“Hello, World!”。使用 System.Reflection.Emit 编程发射器。该代码在没有任何 Emit 异常的情况下工作,但是当我运行生成的“test.exe”文件时,它会抛出异常
Unhandled Exception: System.MissingMethodException: Entry point not found in assembly 'IL_Test, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
我曾尝试在谷歌上搜索答案,但无济于事。也许这里有人可以帮助我?(请)我写的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
using System.IO;
using System.Diagnostics;
namespace ILCompileTest
{
class Program
{
static void Main(string[] args)
{
const string ASSEMBLY_NAME = "IL_Test";
AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
new AssemblyName(ASSEMBLY_NAME), AssemblyBuilderAccess.Save);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(
ASSEMBLY_NAME);
TypeBuilder typeBuilder = moduleBuilder.DefineType("Program",
TypeAttributes.Class | TypeAttributes.Public);
MethodBuilder methodBuilder = typeBuilder.DefineMethod(
"Main", MethodAttributes.Public | MethodAttributes.Static,
typeof(void), new Type[] { typeof(string[]) });
ILGenerator gen = methodBuilder.GetILGenerator();
gen.Emit(OpCodes.Ldstr, "Hello, World!");
gen.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }));
gen.Emit(OpCodes.Ldc_I4_1);
gen.Emit(OpCodes.Call, typeof(Console).GetMethod("ReadKey", new Type[] { typeof(bool) }));
assemblyBuilder.SetEntryPoint(methodBuilder, PEFileKinds.ConsoleApplication);
File.Delete("test.exe");
assemblyBuilder.Save("test.exe");
Process.Start("test.exe");
}
}
}
所以,问题是:如何将入口点设置为我定义的 Main 方法?