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 87959b02..0cb6b15f 100644 Binary files a/src/MemoryPack.Unity/Assets/Plugins/MemoryPack/Runtime/MemoryPack.Generator/MemoryPack.Generator.Roslyn3.dll and b/src/MemoryPack.Unity/Assets/Plugins/MemoryPack/Runtime/MemoryPack.Generator/MemoryPack.Generator.Roslyn3.dll differ