这一篇讲解怎么加载bool值、sbyte值、byte值、char值、short值。
加载bool值
在.NET程序实际运行中,是没有true和false值的,实际上是以1和0表示它们,加载它们的指令是Ldc_I4_1和Ldc_I4_0。
下面是实际例子:
/* 加载true,即加载1 */
ilGenerator.Emit(OpCodes.Ldc_I4_1);
/* 加载false,即加载0 */
ilGenerator.Emit(OpCodes.Ldc_I4_0);
加载sbyte值
sbyte是8位有符号整数类型,它的加载指令是Ldc_I4_S,格式是
ilGenerator.Emit(OpCodes.Ldc_I4_S, <sbyte值>);
例如:
ilGenerator.Emit(OpCodes.Ldc_I4_S,(sbyte)100);
加载byte值
byte是8位无符号整数类型,加载它实际就是加载整数,加载它用加载前面实现的加载int值的函数
LoadInt(ILGenerator il, int value)就可以。
例如:
LoadInt(ilGenerator, (int)(100));
加载char值
char实际上是16位无符号整数类型,加载它也就是加载整数,加载它用加载前面实现的加载int值的函数
LoadInt(ILGenerator il, int value)就可以。
例如:
LoadInt(ilGenerator, (int)('A'));
加载short值
short是16位有符号整数类型,加载它和加载整数一样,与加载char差不多,加载它用加载前面实现的加载int值的函数
LoadInt(ILGenerator il, int value)就可以。
例如:
LoadInt(ilGenerator, (short)(-100));
完整的程序:
using System; using System.Reflection; using System.Reflection.Emit; namespace LX1_ILDemo { /// <summary> /// load char、sbyte、char值 /// </summary> class Demo03_LoadBSBC { static string binaryName = "Demo03_LoadBSBC.exe"; static string namespaceName = "LX1_ILDemo"; static string typeName = "LoadBSBC"; static AssemblyBuilder assemblyBuilder; static ModuleBuilder moduleBuilder; static TypeBuilder typeBuilder; static MethodBuilder mainMethod; static ILGenerator ilGenerator; static void Emit_Ldc() { /* 加载bool值 */ MethodInfo writeBoolLineMethod = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(bool) }); /* 加载true,即加载1 */ ilGenerator.Emit(OpCodes.Ldc_I4_1); ilGenerator.Emit(OpCodes.Call, writeBoolLineMethod); /* 加载false,即加载0 */ ilGenerator.Emit(OpCodes.Ldc_I4_0); ilGenerator.Emit(OpCodes.Call, writeBoolLineMethod); /* 加载sbyte值,用Ldc_I4_S */ MethodInfo writeIntLineMethod = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) }); ilGenerator.Emit(OpCodes.Ldc_I4_S,(sbyte)100); ilGenerator.Emit(OpCodes.Call, writeIntLineMethod); ilGenerator.Emit(OpCodes.Ldc_I4_S, (sbyte)(-1)); ilGenerator.Emit(OpCodes.Call, writeIntLineMethod); ilGenerator.Emit(OpCodes.Ldc_I4_S, sbyte.MaxValue); ilGenerator.Emit(OpCodes.Call, writeIntLineMethod); ilGenerator.Emit(OpCodes.Ldc_I4_S, sbyte.MinValue); ilGenerator.Emit(OpCodes.Call, writeIntLineMethod); /* 加载byte值,和加载整数一样 */ LoadInt(ilGenerator, (int)(101)); ilGenerator.Emit(OpCodes.Call, writeIntLineMethod); LoadInt(ilGenerator, (int)(byte.MaxValue)); ilGenerator.Emit(OpCodes.Call, writeIntLineMethod); LoadInt(ilGenerator, (int)(byte.MinValue)); ilGenerator.Emit(OpCodes.Call, writeIntLineMethod); /* 加载char值,即加载16位无符号整数 */ MethodInfo writeCharLineMethod = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(char) }); LoadInt(ilGenerator, (int)('A')); ilGenerator.Emit(OpCodes.Call, writeCharLineMethod); LoadInt(ilGenerator, (int)('C')); ilGenerator.Emit(OpCodes.Call, writeCharLineMethod); LoadInt(ilGenerator, (int)(char.MaxValue)); ilGenerator.Emit(OpCodes.Call, writeCharLineMethod); LoadInt(ilGenerator, (int)(char.MinValue)); ilGenerator.Emit(OpCodes.Call, writeCharLineMethod); /* 加载short值,和加载整数一样 */ LoadInt(ilGenerator, (short)(-100)); ilGenerator.Emit(OpCodes.Call, writeIntLineMethod); LoadInt(ilGenerator, (short)(100)); ilGenerator.Emit(OpCodes.Call, writeIntLineMethod); LoadInt(ilGenerator, (int)(short.MaxValue)); ilGenerator.Emit(OpCodes.Call, writeIntLineMethod); LoadInt(ilGenerator, (int)(short.MinValue)); ilGenerator.Emit(OpCodes.Call, writeIntLineMethod); } public static void LoadInt(ILGenerator il, int value) { switch (value) { case -1: il.Emit(OpCodes.Ldc_I4_M1); return; case 0: il.Emit(OpCodes.Ldc_I4_0); return; case 1: il.Emit(OpCodes.Ldc_I4_1); return; case 2: il.Emit(OpCodes.Ldc_I4_2); return; case 3: il.Emit(OpCodes.Ldc_I4_3); return; case 4: il.Emit(OpCodes.Ldc_I4_4); return; case 5: il.Emit(OpCodes.Ldc_I4_5); return; case 6: il.Emit(OpCodes.Ldc_I4_6); return; case 7: il.Emit(OpCodes.Ldc_I4_7); return; case 8: il.Emit(OpCodes.Ldc_I4_8); return; } if (value > -129 && value < 128) { il.Emit(OpCodes.Ldc_I4_S, (SByte)value); } else { il.Emit(OpCodes.Ldc_I4, value); } } public static void Generate() { InitAssembly(); /* 生成 public class LoadLFDSN */ typeBuilder = moduleBuilder.DefineType( namespaceName+"."+ typeName, TypeAttributes.Public); /* 生成 public static void Main() */ GenerateMain(); Emit_Ldc(); EmitReadKey(); ilGenerator.Emit(OpCodes.Ret); /* 设置assembly入口方法 */ assemblyBuilder.SetEntryPoint(mainMethod, PEFileKinds.ConsoleApplication); SaveAssembly(); Console.WriteLine("生成成功"); } static void EmitReadKey() { /* 生成 Console.ReadKey(); */ MethodInfo readKeyMethod = typeof(Console).GetMethod("ReadKey", new Type[] { }); ilGenerator.Emit(OpCodes.Call, readKeyMethod); ilGenerator.Emit(OpCodes.Pop); } static void GenerateMain() { mainMethod = typeBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.Static, typeof(void), new Type[] { }); ilGenerator = mainMethod.GetILGenerator(); } 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); } } }