diff --git a/BinarySerializer.Test/Custom/CustomTests.cs b/BinarySerializer.Test/Custom/CustomTests.cs index 5446b2d1..737e9984 100644 --- a/BinarySerializer.Test/Custom/CustomTests.cs +++ b/BinarySerializer.Test/Custom/CustomTests.cs @@ -28,11 +28,12 @@ public void TestCustomDateTime() public void CustomWithContextTest() { var expected = new CustomWithContextContainerClass {Value = new CustomWithContextClass()}; + var context = new CustomWithContextContainerContextClass { Value = "context" }; var serializer = new BinarySerializer(); var stream = new MemoryStream(); - serializer.Serialize(stream, expected, "context"); + serializer.Serialize(stream, expected, context); stream.Position = 0; serializer.Deserialize(stream); } diff --git a/BinarySerializer.Test/Custom/CustomWithContextClass.cs b/BinarySerializer.Test/Custom/CustomWithContextClass.cs index 31ebf4eb..334232b2 100644 --- a/BinarySerializer.Test/Custom/CustomWithContextClass.cs +++ b/BinarySerializer.Test/Custom/CustomWithContextClass.cs @@ -9,8 +9,10 @@ public void Serialize(Stream stream, BinarySerialization.Endianness endianness, BinarySerializationContext serializationContext) { Assert.AreEqual(typeof(CustomWithContextContainerClass), serializationContext.ParentType); - //Assert.AreEqual("context", serializationContext.ParentContext.ParentValue); - // TODO check root context + + + Assert.AreEqual(typeof(CustomWithContextContainerContextClass), serializationContext.ParentContext.ParentContext.ParentType); + Assert.AreEqual("context", (serializationContext.ParentContext.ParentContext.ParentValue as CustomWithContextContainerContextClass).Value); } public void Deserialize(Stream stream, BinarySerialization.Endianness endianness, diff --git a/BinarySerializer.Test/Custom/CustomWithContextContainerContextClass.cs b/BinarySerializer.Test/Custom/CustomWithContextContainerContextClass.cs new file mode 100644 index 00000000..c1dd2983 --- /dev/null +++ b/BinarySerializer.Test/Custom/CustomWithContextContainerContextClass.cs @@ -0,0 +1,7 @@ +namespace BinarySerialization.Test.Custom +{ + public class CustomWithContextContainerContextClass + { + public string Value { get; set; } + } +} \ No newline at end of file diff --git a/BinarySerializer/Graph/ValueGraph/RootValueNode.cs b/BinarySerializer/Graph/ValueGraph/RootValueNode.cs index cc5afdd3..a7d3fbd5 100644 --- a/BinarySerializer/Graph/ValueGraph/RootValueNode.cs +++ b/BinarySerializer/Graph/ValueGraph/RootValueNode.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -101,6 +102,13 @@ protected override Encoding GetFieldEncoding() return EncodingCallback(); } + protected override BinarySerializationContext CreateSerializationContext() + { + var parent = Children.FirstOrDefault()?.Parent; + return new BinarySerializationContext(Value, parent?.Value, parent?.TypeNode.Type, + null, TypeNode.MemberInfo); + } + private static RootTypeNode GetContextGraph(Type valueType) { lock (ContextCacheLock) diff --git a/BinarySerializer/Graph/ValueGraph/ValueNode.cs b/BinarySerializer/Graph/ValueGraph/ValueNode.cs index 67d71f01..c9d86c8f 100644 --- a/BinarySerializer/Graph/ValueGraph/ValueNode.cs +++ b/BinarySerializer/Graph/ValueGraph/ValueNode.cs @@ -960,7 +960,7 @@ private async Task ProcessPaddingAsync(BoundedStream stream, Func