This post originated from an RSS feed registered with .NET Buzz
by Adrian Florea.
Original Post: GetILAsByteArray e l'opzione /bytes dell'ildasm
Feed Title: Web Log di Adrian Florea
Feed URL: /error.aspx?aspxerrorpath=/adrian/Rss.aspx
Feed Description: "You know you've achieved perfection in design, not when you have nothing more to add, but when you have nothing more to take away." Antoine de Saint-Exupery
Per vedere "the actual bytes (in hex) as instruction comments" nel codice IL, abbiamo l'opzione /bytes dell'ildasm. Per esempio, per il metodo Foo::Sum della classe:
classFoo { publicint Sum(int a, int b) { return a + b; } }
dove con rosso sono scritti in esadecimale gli opcode (e i loro parametri! - vedi lo 00 nella colonna a destra) delle istruzioni IL corrispondenti (vedi ECMA-335, Partition III, 1.2.1 "Opcode encodings").
Ottenere questi byte per un metodo o per un costruttore è semplicissimo, basta utilizzare (in .NET 2.0) il metodo GetILAsByteArray: Per esempio, questo snippet:
foreach(byte b intypeof(Foo).GetMethod("Sum").GetMethodBody().GetILAsByteArray()) { Console.WriteLine(Convert.ToString(b, 16).PadLeft(2, '0').ToUpper()); }