2

我正在用 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 方法?

4

1 回答 1

3

您缺少对 typeBuilder.CreateType() 的调用,DefineDynamicModule 必须将 exe 名称作为第二个参数传递。完整的工作样本:

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, "test.exe");
            TypeBuilder typeBuilder = moduleBuilder.DefineType("Program", 
                TypeAttributes.Class | TypeAttributes.Public);
            MethodBuilder methodBuilder = typeBuilder.DefineMethod(
                "Main", MethodAttributes.HideBySig|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) }));
            typeBuilder.CreateType();
            assemblyBuilder.SetEntryPoint(methodBuilder, PEFileKinds.ConsoleApplication);
            File.Delete("test.exe");
            assemblyBuilder.Save("test.exe");

            Process.Start("test.exe");
        }
    }
}
于 2013-03-24T18:14:54.597 回答