2

I am working on some code that parses IL byte arrays as returned by MethodBody.GetILAsByteArray.

Lets say I want to read a metadata token or a 32-bit integer constant from such an IL byte stream. At first I thought using BitConverter.ToInt32(byteArray, offset) would make this easy. However I'm now worried that this won't work on big-endian machines.

As far as I know, IL always uses little-endian encoding for multi-byte values:

"All argument numbers are encoded least-significant-byte-at-smallest-address (a pattern commonly termed 'little-endian')."The Common Language Infrastructure Annotated Standard, Partition III, ch. 1.2 (p. 482).

Since BitConverter's conversion methods honour the computer architecture's endianness (which can be discovered through BitConverter.IsLittleEndian), I conclude that BitConverter should not be used to extract multi-byte values from an IL byte stream, because this would give wrong results on big-endian machines.

Is this conclusion correct?

  • If yes: Is there any way to tell BitConverter which endianness to use for conversions, or is there any other class in the BCL that offers this functionality, or do I have to write my own conversion code?

  • If no: Where am I wrong? What is the proper way of extracting e.g. a Int32 operand value from an IL byte array?

4

1 に答える 1

1

渡す前に、常にリトルエンディアン配列でこれを行う必要があります。

// Array is little. Are we on big?
if (!BitConverter.IsLittleEndian)
{
    // Then flip it
    Array.Reverse(array);
}
int val = BitConverter.ToInt32(...);

しかし、あなたがILストリームについて言及しているように。バイトコードは次のとおりです(AFAIK):

(OPCODE:(1|2):little) (VARIABLES:x:little)

したがって、バイトを読み取り、そのオペコードを確認してから、適切なバイトを読み取り、必要に応じて上記のコードを使用して配列を反転します。何をしているのか聞いてもいいですか?

于 2012-08-12T11:33:59.423 に答える