zoukankan      html  css  js  c++  java
  • 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"的是一样的。

  • 相关阅读:
    关于C#的委托与事件的重新认识
    linux 下添加,修改,删除路由
    反射获取程序集的信息
    原创:2016.4.252016.5.1 C# informal essay and tittle_tattle
    原创:C sharp 中 Enum的几点小 Tips
    转:Dictionary<int,string>怎么获取它的值的集合?急!急!急!
    转:C#整数三种强制类型转换int、Convert.ToInt32()、int.Parse()的区别
    转:C#: static关键字的作用
    新博客 Fighting
    Win10系统下在国内访问Tensorflow官网
  • 原文地址:https://www.cnblogs.com/tkt2016/p/8565877.html
Copyright © 2011-2022 走看看