提交 4d5c7862 编写于 作者: C CyrusNajmabadi

Serialize without unsafe code.

上级 27768b04
......@@ -1036,6 +1036,9 @@ private void TestRoundTripChar(Char ch)
public void TestRoundTripGuid()
{
TestRoundTripGuid(Guid.Empty);
TestRoundTripGuid(new Guid(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1));
TestRoundTripGuid(new Guid(0b10000000000000000000000000000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1));
TestRoundTripGuid(new Guid(0b10000000000000000000000000000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
for (int i = 0; i < 10; i++)
{
TestRoundTripGuid(Guid.NewGuid());
......
......@@ -126,15 +126,13 @@ public void Dispose()
public unsafe Guid ReadGuid()
{
Debug.Assert(sizeof(Guid) == 16);
Debug.Assert(sizeof(long) == 8);
Guid guid;
long* pGuid = (long*)&guid;
pGuid[0] = ReadInt64();
pGuid[1] = ReadInt64();
var accessor = new ObjectWriter.GuidAccessor
{
Low64 = ReadInt64(),
High64 = ReadInt64()
};
return guid;
return accessor.Guid;
}
public object ReadValue()
......
......@@ -119,13 +119,32 @@ public void Dispose()
public void WriteUInt16(ushort value) => _writer.Write(value);
public void WriteString(string value) => WriteStringValue(value);
public unsafe void WriteGuid(Guid guid)
/// <summary>
/// Used so we can easily grab the low/high 64bits of a guid for serialization.
/// </summary>
[StructLayout(LayoutKind.Explicit)]
internal struct GuidAccessor
{
unsafe static GuidAccessor()
{
Debug.Assert(sizeof(Guid) == 16);
Debug.Assert(sizeof(long) == 8);
}
[FieldOffset(0)]
public Guid Guid;
[FieldOffset(0)]
public long Low64;
[FieldOffset(8)]
public long High64;
}
public void WriteGuid(Guid guid)
{
Debug.Assert(sizeof(Guid) == 16);
Debug.Assert(sizeof(long) == 8);
long* pGuid = (long*)&guid;
WriteInt64(pGuid[0]);
WriteInt64(pGuid[1]);
var accessor = new GuidAccessor { Guid = guid };
WriteInt64(accessor.Low64);
WriteInt64(accessor.High64);
}
public void WriteValue(object value)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册