【发布时间】:2016-09-03 08:46:18
【问题描述】:
我有一个看起来像这样的简单对象:
public class Foo
{
public UInt32 One { get; set; }
public UInt32 Two { get; set; }
public UInt32 Three { get; set; }
public UInt32 Four { get; set; }
}
我尝试了我在网上某处找到的这段代码:
public byte[] ObjectToByteArray(Object obj)
{
MemoryStream fs = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, obj);
byte[] rval = fs.ToArray();
fs.Close();
return rval;
}
但不知何故,返回的字节数组的大小为 248 字节。
我希望它是 4 个字节 x 4 个字段 = 16 个字节。
问题:
将固定对象转换为字节数组的最简洁方法是什么?
在这种情况下,结果数组的大小应该是 16 字节吗?
【问题讨论】: