Составители:
Рубрика:
static void Main()
{
testCodec(test1);
testCodec(test2);
}
static void testCodec(byte[] cilStream)
{
Console.WriteLine(“Decoded CIL stream:”);
Instruction[] instrArray = CilCodec.DecodeCil(cilStream);
foreach (Instruction ins in instrArray)
Console.WriteLine(ins);
byte[] cilStream2 = CilCodec.EncodeCil(instrArray);
bool areEqual = cilStream.Length == cilStream2.Length;
for (int i = 0; areEqual && i < cilStream.Length; i++)
areEqual = cilStream[i] == cilStream2[i];
Console.WriteLine(areEqual ?
“Codec is correct” : “Codec is incorrect”);
Console.WriteLine(“---------------------------------------”);
}
#region Sample instruction streams
static byte[] test1 = new byte[]
{
0x23, 0, 0, 0, 0, 0, 0, 0xF0, 0x3F,
/* IL_0000: ldc.r8 1. */
0x0A, /* IL_0009: stloc.0 */
0x2B, 0x1B, /* IL_000a: br.s IL_0027 */
0x03, /* IL_000c: ldarg.1 */
0x18, /* IL_000d: ldc.i4.2 */
0x5D, /* IL_000e: rem */
0x17, /* IL_000f: ldc.i4.1 */
0x33, 0x0B, /* IL_0010: bne.un.s IL_001d */
0x03, /* IL_0012: ldarg.1 */
0x17, /* IL_0013: ldc.i4.1 */
0x59, /* IL_0014: sub */
0x10, 0x01, /* IL_0015: starg.s 1 */
0x06, /* IL_0017: ldloc.0 */
Исходный код программы CilCodec
299
foreach(int target in targets)
{
operands =
BitConverter.GetBytes(target-nextOpOffset);
foreach( byte op in operands )
Result.Add(op);
}
break;
default: /* token */
Result.Add((byte) ((int)ins.Operand >> 24));
Result.Add((byte) ((int)ins.Operand >> 16));
Result.Add((byte) ((int)ins.Operand >> 8));
Result.Add((byte) ((int)ins.Operand));
break;
}
}
return Result.ToArray(typeof(byte)) as byte[];
}
static CilCodec()
{
codes = new Hashtable();
Type opCodesType =
Type.GetType(“System.Reflection.Emit.OpCodes”);
FieldInfo[] fields = opCodesType.GetFields(
(BindingFlags.Static | BindingFlags.Public));
foreach (FieldInfo field in fields)
{
OpCode opCode = (OpCode)(field.GetValue(null));
codes.Add(opCode.Value,opCode);
}
}
private static Hashtable codes;
}
class Demo
{
298
CIL и системное программирование в Microsoft .NET
298 CIL и системное программирование в Microsoft .NET Исходный код программы CilCodec 299 foreach(int target in targets) static void Main() { { operands = testCodec(test1); BitConverter.GetBytes(target-nextOpOffset); testCodec(test2); foreach( byte op in operands ) } Result.Add(op); } static void testCodec(byte[] cilStream) break; { Console.WriteLine(“Decoded CIL stream:”); default: /* token */ Result.Add((byte) ((int)ins.Operand >> 24)); Instruction[] instrArray = CilCodec.DecodeCil(cilStream); Result.Add((byte) ((int)ins.Operand >> 16)); foreach (Instruction ins in instrArray) Result.Add((byte) ((int)ins.Operand >> 8)); Console.WriteLine(ins); Result.Add((byte) ((int)ins.Operand)); break; byte[] cilStream2 = CilCodec.EncodeCil(instrArray); } bool areEqual = cilStream.Length == cilStream2.Length; } for (int i = 0; areEqual && i < cilStream.Length; i++) areEqual = cilStream[i] == cilStream2[i]; return Result.ToArray(typeof(byte)) as byte[]; } Console.WriteLine(areEqual ? “Codec is correct” : “Codec is incorrect”); static CilCodec() Console.WriteLine(“---------------------------------------”); { } codes = new Hashtable(); #region Sample instruction streams Type opCodesType = Type.GetType(“System.Reflection.Emit.OpCodes”); static byte[] test1 = new byte[] FieldInfo[] fields = opCodesType.GetFields( { (BindingFlags.Static | BindingFlags.Public)); 0x23, 0, 0, 0, 0, 0, 0, 0xF0, 0x3F, /* IL_0000: ldc.r8 1. */ foreach (FieldInfo field in fields) 0x0A, /* IL_0009: stloc.0 */ { 0x2B, 0x1B, /* IL_000a: br.s IL_0027 */ OpCode opCode = (OpCode)(field.GetValue(null)); 0x03, /* IL_000c: ldarg.1 */ codes.Add(opCode.Value,opCode); 0x18, /* IL_000d: ldc.i4.2 */ } 0x5D, /* IL_000e: rem */ } 0x17, /* IL_000f: ldc.i4.1 */ 0x33, 0x0B, /* IL_0010: bne.un.s IL_001d */ private static Hashtable codes; 0x03, /* IL_0012: ldarg.1 */ } 0x17, /* IL_0013: ldc.i4.1 */ 0x59, /* IL_0014: sub */ class Demo 0x10, 0x01, /* IL_0015: starg.s 1 */ { 0x06, /* IL_0017: ldloc.0 */
Страницы
- « первая
- ‹ предыдущая
- …
- 154
- 155
- 156
- 157
- 158
- …
- следующая ›
- последняя »