C#支持的位运算是与、或、异或、取反、左移、右移,它们对应的指令是
And、Or、Xor、Not、Shl、Shr。取反运算只需要一个操作数,生成步骤是
1.生成加载变量2.生成取反指令实例代码:ilGenerator.Emit(OpCodes.Ldarg_0);ilGenerator.Emit(OpCodes.Not);
其它五个运算都需要两个参数,它们的通用步骤
1.生成加载左边变量2.生成加载右边变量3.生成运算指令实例:
ilGenerator.Emit(OpCodes.Ldarg_0);ilGenerator.Emit(OpCodes.Ldarg_1);ilGenerator.Emit(OpCodes.And);
完整的程序如下:
using System;using System.Reflection;using System.Reflection.Emit;namespace LX1_ILDemo{ class Demo17_BitOP { static string binaryName = "Demo17_BitOP.exe"; static string namespaceName = "LX1_ILDemo"; static string typeName = "BitOPDemo"; static AssemblyBuilder assemblyBuilder; static ModuleBuilder moduleBuilder; static TypeBuilder typeBuilder; static MethodBuilder mainMethod; static MethodBuilder testMethod; static void Emit_Test() { testMethod = typeBuilder.DefineMethod("TestOP", MethodAttributes.Public | MethodAttributes.Static, typeof(void), new Type[] { typeof(int), typeof(int) }); var println = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) }); var ilGenerator = testMethod.GetILGenerator(); ilGenerator.Emit(OpCodes.Ldarg_0); ilGenerator.Emit(OpCodes.Not); ilGenerator.Emit(OpCodes.Call, println); ilGenerator.Emit(OpCodes.Ldarg_0); ilGenerator.Emit(OpCodes.Ldarg_1); ilGenerator.Emit(OpCodes.And); ilGenerator.Emit(OpCodes.Call, println); ilGenerator.Emit(OpCodes.Ldarg_0); ilGenerator.Emit(OpCodes.Ldarg_1); ilGenerator.Emit(OpCodes.Or); ilGenerator.Emit(OpCodes.Call, println); ilGenerator.Emit(OpCodes.Ldarg_0); ilGenerator.Emit(OpCodes.Ldarg_1); ilGenerator.Emit(OpCodes.Xor); ilGenerator.Emit(OpCodes.Call, println); ilGenerator.Emit(OpCodes.Ldarg_0); ilGenerator.Emit(OpCodes.Ldc_I4_2); ilGenerator.Emit(OpCodes.Shl); ilGenerator.Emit(OpCodes.Call, println); ilGenerator.Emit(OpCodes.Ldarg_0); ilGenerator.Emit(OpCodes.Ldc_I4_2); ilGenerator.Emit(OpCodes.Shr); ilGenerator.Emit(OpCodes.Call, println); ilGenerator.Emit(OpCodes.Ret); } public static void Generate() { InitAssembly(); typeBuilder = moduleBuilder.DefineType(namespaceName + "." + typeName, TypeAttributes.Public); Emit_Test(); GenerateMain(); assemblyBuilder.SetEntryPoint(mainMethod, PEFileKinds.ConsoleApplication); SaveAssembly(); Console.WriteLine("生成成功"); } static void GenerateMain() { mainMethod = typeBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.Static, typeof(void), new Type[] { }); var ilGenerator = mainMethod.GetILGenerator(); ilGenerator.Emit(OpCodes.Ldc_I4,1000); ilGenerator.Emit(OpCodes.Ldc_I4,120); ilGenerator.Emit(OpCodes.Call, testMethod); ilGenerator.Emit(OpCodes.Call, typeof(Console).GetMethod("ReadKey", new Type[] { })); ilGenerator.Emit(OpCodes.Pop); ilGenerator.Emit(OpCodes.Ret); } 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); } }}