Skip to content

Commit f6d05e6

Browse files
committed
fix #166
1 parent 39a9f6c commit f6d05e6

4 files changed

Lines changed: 39 additions & 0 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,20 @@ public partial class MyStringDictionary<TValue> : Dictionary<string, TValue>
335335
}
336336
```
337337

338+
Static constructor
339+
---
340+
MemoryPackable class can not define static constructor because generated partial class uses it. Instead, you can define a `static partial void StaticConstructor()` to do the same thing.
341+
342+
```csharp
343+
[MemoryPackable]
344+
public partial class CctorSample
345+
{
346+
static partial void StaticConstructor()
347+
{
348+
}
349+
}
350+
```
351+
338352
Polymorphism (Union)
339353
---
340354
MemoryPack supports serializing interface and abstract class objects for polymorphism serialization. In MemoryPack this feature is called Union. Only interfaces and abstracts classes are allowed to be annotated with `[MemoryPackUnion]` attributes. Unique union tags are required.

sandbox/SandboxConsoleApp/Program.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,26 @@
4747

4848
var span = CollectionsMarshal.AsSpan(value.Payload);
4949

50+
51+
[MemoryPackable]
52+
public partial class CctorSample
53+
{
54+
static partial void StaticConstructor()
55+
{
56+
}
57+
}
58+
5059
[MemoryPackable]
5160
public partial class ListBytesSample
5261
{
5362
public int Id { get; set; }
5463
public List<byte> Payload { get; set; }
64+
65+
static partial void StaticConstructor()
66+
{
67+
Console.WriteLine("foo");
68+
// throw new NotImplementedException();
69+
}
5570
}
5671

5772
[MemoryPackable]

src/MemoryPack.Generator/MemoryPackGenerator.Emitter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,12 @@ public void Emit(StringBuilder writer, IGeneratorContext context)
350350
partial {{classOrStructOrRecord}} {{TypeName}} : IMemoryPackable<{{TypeName}}>{{fixedSizeInterface}}
351351
{
352352
{{EmitCustomFormatters()}}
353+
static partial void StaticConstructor();
353354
354355
static {{Symbol.Name}}()
355356
{
356357
{{registerT}}
358+
StaticConstructor();
357359
}
358360
{{fixedSizeMethod}}
359361
[global::MemoryPack.Internal.Preserve]
@@ -1194,9 +1196,12 @@ string EmitGenericCollectionTemplate(IGeneratorContext context)
11941196
var code = $$"""
11951197
partial class {{TypeName}} : IMemoryPackFormatterRegister
11961198
{
1199+
static partial void StaticConstructor();
1200+
11971201
static {{Symbol.Name}}()
11981202
{
11991203
{{register}}
1204+
StaticConstructor();
12001205
}
12011206
12021207
{{staticRegisterFormatterMethod}}RegisterFormatter()

src/MemoryPack.Generator/TypeScriptRuntime.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@ public writeString(value: string | null): void {
283283
return;
284284
}
285285
286+
if (value.length == 0) {
287+
this.writeCollectionHeader(0);
288+
return;
289+
}
290+
286291
// (int ~utf8-byte-count, int utf16-length, utf8-bytes)
287292
this.ensureCapacity(8 + ((value.length + 1) * 3));
288293

0 commit comments

Comments
 (0)