⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 23.6.txt

📁 《Microsoft Visual C# .NET 2003开发技巧大全》源代码
💻 TXT
字号:
Listing 23.6 Emitting IL Code
// add the hoarse method
MethodBuilder hoarseMethod = typeBuilder.DefineMethod( “MakeHoarse”,
MethodAttributes.Public, null, new Type[]{} );
ILGenerator gen = hoarseMethod.GetILGenerator();
// create labels for branching
Label endLoop = gen.DefineLabel();
Label beginLoop = gen.DefineLabel();
// create Console.WriteLine MethodInfo
MethodInfo writeLine = typeof(Console).GetMethod( “WriteLine”,
new Type[]{typeof(string)});
gen.DeclareLocal( typeof(int) );
gen.Emit( OpCodes.Ldc_I4_0 );
gen.Emit( OpCodes.Stloc_0 );
gen.Emit( OpCodes.Br_S, endLoop );
gen.MarkLabel( beginLoop );
gen.Emit( OpCodes.Ldarg_0 );
gen.Emit( OpCodes.Ldfld, soundFld );
gen.EmitCall( OpCodes.Call, writeLine, null );
gen.Emit( OpCodes.Ldloc_0 );
gen.Emit( OpCodes.Ldc_I4_1 );
gen.Emit( OpCodes.Add );
gen.Emit( OpCodes.Stloc_0 );
gen.MarkLabel( endLoop );
gen.Emit( OpCodes.Ldloc_0 );
gen.Emit( OpCodes.Ldarg_0 );
gen.Emit( OpCodes.Ldfld, countFld );
gen.Emit( OpCodes.Blt_S, beginLoop );
gen.EmitWriteLine(“...a muffled sound emanates...” );
gen.Emit( OpCodes.Ret );
// MakeHoarse IL Code
/*
IL_0000: ldc.i4.0
IL_0001: stloc.0
IL_0002: br.s IL_0013
IL_0004: ldarg.0
IL_0005: ldfld string ConsoleApplication1.Horse::Sound
IL_000a: call void [mscorlib]System.Console::WriteLine(string)
IL_000f: ldloc.0
IL_0010: ldc.i4.1
IL_0011: add
IL_0012: stloc.0
IL_0013: ldloc.0
IL_0014: ldarg.0
IL_0015: ldfld int32 ConsoleApplication1.Horse::Count
IL_001a: blt.s IL_0004
IL_001c: ldstr “...a muffled sound emanates from the animal.”
IL_0021: call void [mscorlib]System.Console::WriteLine(string)
IL_0026: ret
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -