Skip to content

Commit a59a63e

Browse files
committed
r
1 parent 9e4d976 commit a59a63e

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public partial class Person3
207207

208208
### Serialization callbacks
209209

210-
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.
210+
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.
211211

212212
```csharp
213213
[MemoryPackable]
@@ -267,6 +267,50 @@ public partial class MethodCallSample
267267
}
268268
```
269269

270+
Callbacks allows parameterless method and `ref reader/writer, ref T value` method. For example, ref callbacks can write/read custom header before serialization process.
271+
272+
```csharp
273+
[MemoryPackable]
274+
public partial class EmitIdData
275+
{
276+
public int MyProperty { get; set; }
277+
278+
[MemoryPackOnSerializing]
279+
static void WriteId<TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer, ref EmitIdData? value)
280+
where TBufferWriter : IBufferWriter<byte> // .NET Standard 2.1, use where TBufferWriter : class, IBufferWriter<byte>
281+
{
282+
writer.WriteUnmanaged(Guid.NewGuid()); // emit GUID in header.
283+
}
284+
285+
[MemoryPackOnDeserializing]
286+
static void ReadId(ref MemoryPackReader reader, ref EmitIdData? value)
287+
{
288+
// read custom header before deserialize
289+
var guid = reader.ReadUnmanaged<Guid>();
290+
Console.WriteLine(guid);
291+
}
292+
}
293+
```
294+
295+
If set a value to `ref value`, you can change the value used for serialization/deserialization. For example, instantiate from ServiceProvider.
296+
297+
```csharp
298+
[MemoryPackable]
299+
public partial class InstantiateFromServiceProvider
300+
{
301+
static IServiceProvider serviceProvider = default!;
302+
303+
public int MyProperty { get; private set; }
304+
305+
[MemoryPackOnDeserializing]
306+
static void OnDeserializing(ref MemoryPackReader reader, ref InstantiateFromServiceProvider value)
307+
{
308+
if (value != null) return;
309+
value = serviceProvider.GetRequiredService<InstantiateFromServiceProvider>();
310+
}
311+
}
312+
```
313+
270314
Define custom collection
271315
---
272316
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.

0 commit comments

Comments
 (0)