これを行うよりかわいい方法はありますか?バイトストリームを指定して、目的の数値型に変換します。
(呼び出しコードは、ストリーム内のバイト数に関連するデータ型を処理すると仮定します)。
public void GetValue(byte[] bytes, ref UInt16 value)
{
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);
value = BitConverter.ToUInt16(bytes, 0);
}
public void GetValue(byte[] bytes, ref UInt32 value)
{
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);
value = BitConverter.ToUInt32(bytes, 0);
}
public void GetValue(byte[] bytes, ref UInt64 value)
{
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);
value = BitConverter.ToUInt64(bytes, 0);
}
etc...
たとえば、オーバーロードを複製するのではなく、値のタイプをオンにすることにより、より良い方法があると思います。