From a59a63e175427b1583f940a73dfc718a3878dcce Mon Sep 17 00:00:00 2001 From: neuecc Date: Thu, 8 Dec 2022 19:42:06 +0900 Subject: [PATCH] r --- README.md | 46 +++++++++++++++++- .../MemoryPack.Generator.Roslyn3.dll | Bin 118272 -> 118272 bytes 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 87f93433..342af657 100644 --- a/README.md +++ b/README.md @@ -207,7 +207,7 @@ public partial class Person3 ### Serialization callbacks -When serializing/deserializing, MemoryPack can invoke a before/after event using the `[MemoryPackOnSerializing]`, `[MemoryPackOnSerialized]`, `[MemoryPackOnDeserializing]`, `[MemoryPackOnDeserialized]` attributes. It can annotate both static and instance (non-static) methods, and public and private methods. The only requirement is that the annotated method is parameterless. +When serializing/deserializing, MemoryPack can invoke a before/after event using the `[MemoryPackOnSerializing]`, `[MemoryPackOnSerialized]`, `[MemoryPackOnDeserializing]`, `[MemoryPackOnDeserialized]` attributes. It can annotate both static and instance (non-static) methods, and public and private methods. ```csharp [MemoryPackable] @@ -267,6 +267,50 @@ public partial class MethodCallSample } ``` +Callbacks allows parameterless method and `ref reader/writer, ref T value` method. For example, ref callbacks can write/read custom header before serialization process. + +```csharp +[MemoryPackable] +public partial class EmitIdData +{ + public int MyProperty { get; set; } + + [MemoryPackOnSerializing] + static void WriteId(ref MemoryPackWriter writer, ref EmitIdData? value) + where TBufferWriter : IBufferWriter // .NET Standard 2.1, use where TBufferWriter : class, IBufferWriter + { + writer.WriteUnmanaged(Guid.NewGuid()); // emit GUID in header. + } + + [MemoryPackOnDeserializing] + static void ReadId(ref MemoryPackReader reader, ref EmitIdData? value) + { + // read custom header before deserialize + var guid = reader.ReadUnmanaged(); + Console.WriteLine(guid); + } +} +``` + +If set a value to `ref value`, you can change the value used for serialization/deserialization. For example, instantiate from ServiceProvider. + +```csharp +[MemoryPackable] +public partial class InstantiateFromServiceProvider +{ + static IServiceProvider serviceProvider = default!; + + public int MyProperty { get; private set; } + + [MemoryPackOnDeserializing] + static void OnDeserializing(ref MemoryPackReader reader, ref InstantiateFromServiceProvider value) + { + if (value != null) return; + value = serviceProvider.GetRequiredService(); + } +} +``` + Define custom collection --- By default, annotated `[MemoryPackObject]` type try to serialize its members. However, if a type is a collection (`ICollection<>`, `ISet<>`, `IDictionary<,>`), use `GenerateType.Collection` to serialize it correctly. diff --git a/src/MemoryPack.Unity/Assets/Plugins/MemoryPack/Runtime/MemoryPack.Generator/MemoryPack.Generator.Roslyn3.dll b/src/MemoryPack.Unity/Assets/Plugins/MemoryPack/Runtime/MemoryPack.Generator/MemoryPack.Generator.Roslyn3.dll index 87959b02c8a7dd804f1c1e766f365c753b81eb84..0cb6b15f3d86fd0428d45442bfba46ed142bacac 100644 GIT binary patch delta 253 zcmZpe!`?85eL@GzBd4A-6MKA^{-0^Qwe=R`qSXR|!7IeLSGIWf?LP44!M&Tk+k4h9 zo)N1rDsHX}5D@>r_}!ifkE`BTyr>YD5{lDRH!PZZqGZCq7y9LK#Y((V`=lB7c~>@; zKFICpIR4f^t=?FC+dUb<o>dzj&d@QU1)A&c^2xd@hRi|F~Pb{aIXh zy3E4<`iWBg5UnL{!f2!m@z=0YjORL yd(SjHH_7&0@>ft;Hr?R};}iv`Xk`&p6r{dw$%LR;tSyqsj#2x!e>uX)!wCTYR(YQQ delta 253 zcmZpe!`?85eL@Gz#Hy$BC-(R-g@rWU+Iov|(Q1K=0v6j&H9_wU`?tS78!5YMd(Rri zGh+4Y`P{$lJ{dM4GtAb#S+};&L+!uyyy5m!xp3i;@6#QQFiuf`iuRp_ih|Tje*5{~QXrv?rTyF7?O%>C@^AtG2nTmv