MSIL实用指南-IL版hello world

我们学习编程开始时,一般用输出"hello world"的一段程序。

C#版的"hello world"是

using System;

namespace L01_HelloDemo
{
    class HelloWorld
    {
        class Hello
        {
            public static void Main2()
            {
                Console.WriteLine("Hello World!");
                Console.ReadKey();
            }
        }
    }
}

 

用生成IL方式也可以输出"hello world",它的实现源码是

using System;
using System.Reflection;
using System.Reflection.Emit;

namespace L01_HelloDemo
{
    class HelloWorldILGenerator
    {
        static string binaryName = "ILHelloWorldDemo.exe";
        static string namespaceName = "ILHelloWorldDemo";
        static string typeName = "HelloWorld";
        
        static AssemblyBuilder assemblyBuilder;
        static ModuleBuilder moduleBuilder;
        static TypeBuilder typeBuilder;

        public static void Generate()
        {
            InitAssembly();

            /* 生成 public class HelloWorld */
            typeBuilder = moduleBuilder.DefineType(typeName, TypeAttributes.Public);

            /* 生成 public static void Main() */
            MethodBuilder mainMethod = typeBuilder.DefineMethod("Main",MethodAttributes.Public | MethodAttributes.Static, typeof(void), new Type[] { });
            ILGenerator methIL = mainMethod.GetILGenerator();

            /* 生成 Console.WriteLine("Hello World!"); */
            MethodInfo writeLineMethod = typeof(Console).GetMethod("WriteLine",new Type[]{ typeof(string) });
            methIL.Emit(OpCodes.Ldstr, "Hello World!");
            methIL.Emit(OpCodes.Call, writeLineMethod);

            /* 生成 Console.ReadKey(); */
            MethodInfo readKeyMethod = typeof(Console).GetMethod("ReadKey", new Type[] { });
            methIL.Emit(OpCodes.Call, readKeyMethod);
            methIL.Emit(OpCodes.Pop);

            methIL.Emit(OpCodes.Ret);

            /*  设置assembly入口方法 */
            assemblyBuilder.SetEntryPoint(mainMethod, PEFileKinds.ConsoleApplication);

            SaveAssembly();
            Console.WriteLine("生成成功");
            Console.ReadKey();
        }

        static void InitAssembly()
        {
            AssemblyName assemblyName = new AssemblyName(namespaceName);
            assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
            moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name, binaryName);
        }

        static void SaveAssembly()
        {
            Type t = typeBuilder.CreateType(); //完成Type,这是必须的
            assemblyBuilder.Save(binaryName);
        }
    }
}

 

调用 HelloWorldILGenerator.Generate()后生成ILHelloWorldDemo.exe文件,打开它运行的效果和C#版的"hello world"的是一样的,

用ILSpy对ILHelloWorldDemo.exe进行反编译,反编译的源码也和和C#版的"hello world"的是一样的。

 

posted @ 2018-03-14 10:43  Z语言  阅读(243)  评论(0编辑  收藏  举报