鉴于以下信息
Public Enum Request As Byte
None = 0
Identity = 1
License = 2
End Enum
Protected mType As Communication.Request
mType = Communication.Request.Identity
Debug.Print (BitConverter.GetBytes(mType).Length.tostring)
2
Run Code Online (Sandbox Code Playgroud)
为什么bitconverter报告mType是2的长度.我原以为将Byte传递给BitConverter.GetBytes只会返回字节.
我的意思是没什么大不了的,因为它只是在TCP Socket上发送一个非常小的数据块,但我很感兴趣为什么它认为它是2个字节.
因为BitConverter.GetBytes(字节b)没有过载(参见msdn),所以使用最近的可用隐式重载,在这种情况下,返回一个字节[2].
使用简单的代码:
byte b = 1;
BitConverter.GetBytes(b);
Run Code Online (Sandbox Code Playgroud)
编译它并使用ildasm,我们看到调用int16(这是一个简短的)的方法:
.method public hidebysig instance void bytetest() cil managed
{
// Code size 11 (0xb)
.maxstack 1
.locals init ([0] uint8 b)
IL_0000: nop
IL_0001: ldc.i4.1
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: call uint8[] [mscorlib]System.BitConverter::GetBytes(int16)
IL_0009: pop
IL_000a: ret
} // end of method Test::bytetest
Run Code Online (Sandbox Code Playgroud)
在Visual Studio中悬停方法时也可以看到(选定的重载显示在工具提示中)
| 归档时间: |
|
| 查看次数: |
1411 次 |
| 最近记录: |