You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+23-1Lines changed: 23 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -213,7 +213,7 @@ public partial class Person3
213
213
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.
214
214
215
215
```csharp
216
-
[MemoryPackable]
216
+
[MemoryPackable]
217
217
publicpartialclassMethodCallSample
218
218
{
219
219
// method call order is static -> instance
@@ -546,6 +546,28 @@ public partial class VersionCheck
546
546
547
547
In use-case, store old data (to file, to redis, etc...) and read to new schema is always ok. In the RPC scenario, schema exists both on the client and the server side, the client must be updated before the server. An updated client has no problem connecting to the old server but an old client can not connect to a new server.
548
548
549
+
550
+
By default, when the old data read to new schema, any members not on the data side are initialized with the `default` literal.
551
+
If you want to avoid this and use initial values of field/properties, you can use `[SuppressDefaultInitialization]`.
552
+
553
+
```cs
554
+
[MemoryPackable]
555
+
publicpartialclassDefaultValue
556
+
{
557
+
publicstringProp1 { get; set; }
558
+
559
+
[SuppressDefaultInitialization]
560
+
publicintProp2 { get; set; } =111; // < if old data is missing, set `111`.
561
+
562
+
publicintProp3 { get; set; } =222; // < if old data is missing, set `default`.
563
+
}
564
+
```
565
+
566
+
`[SuppressDefaultInitialization]` has following disadvantages:
567
+
- Cannot be used with readonly, init-only, and required modifier.
568
+
=- May not be the best performance due to increased conditional branching. (But it would be negligible.)
569
+
570
+
549
571
The next [Serialization info](#serialization-info) section shows how to check for schema changes, e.g., by CI, to prevent accidents.
550
572
551
573
When using `GenerateType.VersionTolerant`, it supports full version-tolerant.
0 commit comments