编辑:好的,我终于明白你真正在问什么了。我们通常不使用MemoryMappedViewAccessor
序列化对象,现在你知道为什么了。
以下将为您提供预期的结果。
public static class ByteSerializer
{
public static Byte[] Serialize<T>(IEnumerable<T> msg) where T : struct
{
List<byte> res = new List<byte>();
foreach (var s in msg)
{
res.AddRange(Serialize(s));
}
return res.ToArray();
}
public static Byte[] Serialize<T>(T msg) where T : struct
{
int objsize = Marshal.SizeOf(typeof(T));
Byte[] ret = new Byte[objsize];
IntPtr buff = Marshal.AllocHGlobal(objsize);
Marshal.StructureToPtr(msg, buff, true);
Marshal.Copy(buff, ret, 0, objsize);
Marshal.FreeHGlobal(buff);
return ret;
}
}
class Program
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct Yours
{
public Int64 int1;
public DateTime dt1;
public float f1;
public float f2;
public float f3;
public byte b;
}
static void Main()
{
var file = @"c:\temp\test.bin";
IEnumerable<Yours> t = new Yours[3];
File.WriteAllBytes(file, ByteSerializer.Serialize(t));
using (var stream = File.OpenRead(file))
{
Console.WriteLine("file size: " + stream.Length);
}
}
}
编辑:所以看起来 DateTime 真的很喜欢在对齐的内存地址上。虽然您可以定义显式布局,但我认为更简单的方法是:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Test
{
private long dt1;
public byte b;
public Int64 int1;
public float f1;
public float f2;
public float f3;
public DateTime DT
{
get { return new DateTime(dt1); }
set { dt1 = value.Ticks; }
}
}
虽然我不明白为什么你应该关心托管内存表示。
或者,[StructLayout(LayoutKind.Explicit)]
应该防止内存对齐。
示例(“托管 sizeof”取自这篇文章)
[StructLayout(LayoutKind.Explicit, Size = 9)]
public struct Test
{
[FieldOffset(0)]
public DateTime dt1;
[FieldOffset(8)]
public byte b;
}
class Program
{
static readonly Func<Type, uint> SizeOfType = (Func<Type, uint>)Delegate.CreateDelegate(typeof(Func<Type, uint>), typeof(Marshal).GetMethod("SizeOfType", BindingFlags.NonPublic | BindingFlags.Static));
static void Main()
{
Test t = new Test() { dt1 = DateTime.MaxValue, b = 42 };
Console.WriteLine("Managed size: " + SizeOfType(typeof(Test)));
Console.WriteLine("Unmanaged size: " + Marshal.SizeOf(t));
using (MemoryMappedFile file = MemoryMappedFile.CreateNew(null, 1))
using (MemoryMappedViewAccessor accessor = file.CreateViewAccessor())
{
accessor.Write(0L, ref t);
long pos = 0;
for (int i = 0; i < 9; i++)
Console.Write("|" + accessor.ReadByte(pos++));
Console.Write("|\n");
}
}
}
输出:
Managed size: 9
Unmanaged size: 9
|255|63|55|244|117|40|202|43|42| // managed memory layout is as expected
顺便说一句,DateTime 似乎打破了顺序合同 - 但请记住合同是针对编组内存映射的。没有关于托管内存布局的规范。
[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 9)]
public struct Test
{
public DateTime dt1;
public byte b;
}
以上代码的输出:
Managed size: 12
Unmanaged size: 9
|42|0|0|0|255|63|55|244|117|40|202|43| // finally found those 3 missing bytes :-)