diff --git a/sandbox/SandboxWebApp/wwwroot/js/memorypack/AllConvertableType.ts b/sandbox/SandboxWebApp/wwwroot/js/memorypack/AllConvertableType.ts index 1ca0ec9d..5c573692 100644 --- a/sandbox/SandboxWebApp/wwwroot/js/memorypack/AllConvertableType.ts +++ b/sandbox/SandboxWebApp/wwwroot/js/memorypack/AllConvertableType.ts @@ -1,9 +1,9 @@ import { MemoryPackWriter } from "./MemoryPackWriter.js"; import { MemoryPackReader } from "./MemoryPackReader.js"; -import { NoMarkByteEnum } from "./NoMarkByteEnum.js"; -import { NumberedUShortEnum } from "./NumberedUShortEnum.js"; -import { NestedObject } from "./NestedObject.js"; -import { IMogeUnion } from "./IMogeUnion.js"; +import { NoMarkByteEnum } from "./NoMarkByteEnum.js"; +import { NumberedUShortEnum } from "./NumberedUShortEnum.js"; +import { NestedObject } from "./NestedObject.js"; +import { IMogeUnion } from "./IMogeUnion.js"; export class AllConvertableType { myBool: boolean; diff --git a/sandbox/SandboxWebApp/wwwroot/js/memorypack/ArrayGenericsCheck.ts b/sandbox/SandboxWebApp/wwwroot/js/memorypack/ArrayGenericsCheck.ts index 1705dd25..e4ab4a64 100644 --- a/sandbox/SandboxWebApp/wwwroot/js/memorypack/ArrayGenericsCheck.ts +++ b/sandbox/SandboxWebApp/wwwroot/js/memorypack/ArrayGenericsCheck.ts @@ -1,8 +1,8 @@ import { MemoryPackWriter } from "./MemoryPackWriter.js"; import { MemoryPackReader } from "./MemoryPackReader.js"; -import { NoMarkByteEnum } from "./NoMarkByteEnum.js"; -import { NestedObject } from "./NestedObject.js"; -import { IMogeUnion } from "./IMogeUnion.js"; +import { NoMarkByteEnum } from "./NoMarkByteEnum.js"; +import { NestedObject } from "./NestedObject.js"; +import { IMogeUnion } from "./IMogeUnion.js"; export class ArrayGenericsCheck { array1: (NestedObject | null)[] | null; diff --git a/sandbox/SandboxWebApp/wwwroot/js/memorypack/Person.ts b/sandbox/SandboxWebApp/wwwroot/js/memorypack/Person.ts index 80d353e7..1c86d5f5 100644 --- a/sandbox/SandboxWebApp/wwwroot/js/memorypack/Person.ts +++ b/sandbox/SandboxWebApp/wwwroot/js/memorypack/Person.ts @@ -1,6 +1,6 @@ import { MemoryPackWriter } from "./MemoryPackWriter.js"; import { MemoryPackReader } from "./MemoryPackReader.js"; -import { Gender } from "./Gender.js"; +import { Gender } from "./Gender.js"; export class Person { id: string; diff --git a/sandbox/SandboxWebApp/wwwroot/js/memorypack/SampleUnion1.ts b/sandbox/SandboxWebApp/wwwroot/js/memorypack/SampleUnion1.ts index 0d5c5124..24b2e2f7 100644 --- a/sandbox/SandboxWebApp/wwwroot/js/memorypack/SampleUnion1.ts +++ b/sandbox/SandboxWebApp/wwwroot/js/memorypack/SampleUnion1.ts @@ -1,6 +1,6 @@ import { MemoryPackWriter } from "./MemoryPackWriter.js"; import { MemoryPackReader } from "./MemoryPackReader.js"; -import { IMogeUnion } from "./IMogeUnion.js"; +import { IMogeUnion } from "./IMogeUnion.js"; export class SampleUnion1 implements IMogeUnion { myProperty: number | null; diff --git a/sandbox/SandboxWebApp/wwwroot/js/memorypack/SampleUnion2.ts b/sandbox/SandboxWebApp/wwwroot/js/memorypack/SampleUnion2.ts index 0976da2c..1cea682d 100644 --- a/sandbox/SandboxWebApp/wwwroot/js/memorypack/SampleUnion2.ts +++ b/sandbox/SandboxWebApp/wwwroot/js/memorypack/SampleUnion2.ts @@ -1,6 +1,6 @@ import { MemoryPackWriter } from "./MemoryPackWriter.js"; import { MemoryPackReader } from "./MemoryPackReader.js"; -import { IMogeUnion } from "./IMogeUnion.js"; +import { IMogeUnion } from "./IMogeUnion.js"; export class SampleUnion2 implements IMogeUnion { myProperty: string | null; diff --git a/src/MemoryPack.Core/Internal/TypeHelpers.cs b/src/MemoryPack.Core/Internal/TypeHelpers.cs index 56ec170f..e40afeb4 100644 --- a/src/MemoryPack.Core/Internal/TypeHelpers.cs +++ b/src/MemoryPack.Core/Internal/TypeHelpers.cs @@ -7,6 +7,7 @@ namespace MemoryPack.Internal; internal static class TypeHelpers { static readonly MethodInfo isReferenceOrContainsReferences = typeof(RuntimeHelpers).GetMethod("IsReferenceOrContainsReferences")!; + static readonly MethodInfo unsafeSizeOf = typeof(Unsafe).GetMethod("SizeOf")!; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool IsReferenceOrNullable() @@ -67,7 +68,7 @@ static Cache() if (!containsReference) { IsUnmanagedSZArray = true; - UnmanagedSZArrayElementSize = Marshal.SizeOf(elementType!); + UnmanagedSZArrayElementSize = (int)unsafeSizeOf.MakeGenericMethod(elementType!).Invoke(null, null)!; } } #if NET7_0_OR_GREATER diff --git a/src/MemoryPack.Unity/Assets/Plugins/MemoryPack/Runtime/MemoryPack.Core/Internal/TypeHelpers.cs b/src/MemoryPack.Unity/Assets/Plugins/MemoryPack/Runtime/MemoryPack.Core/Internal/TypeHelpers.cs index 76ef009c..95615888 100644 --- a/src/MemoryPack.Unity/Assets/Plugins/MemoryPack/Runtime/MemoryPack.Core/Internal/TypeHelpers.cs +++ b/src/MemoryPack.Unity/Assets/Plugins/MemoryPack/Runtime/MemoryPack.Core/Internal/TypeHelpers.cs @@ -16,6 +16,7 @@ namespace MemoryPack.Internal { internal static class TypeHelpers { static readonly MethodInfo isReferenceOrContainsReferences = typeof(RuntimeHelpers).GetMethod("IsReferenceOrContainsReferences")!; + static readonly MethodInfo unsafeSizeOf = typeof(Unsafe).GetMethod("SizeOf")!; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool IsReferenceOrNullable() @@ -76,7 +77,7 @@ static Cache() if (!containsReference) { IsUnmanagedSZArray = true; - UnmanagedSZArrayElementSize = Marshal.SizeOf(elementType!); + UnmanagedSZArrayElementSize = (int)unsafeSizeOf.MakeGenericMethod(elementType!).Invoke(null, null)!; } } #if NET7_0_OR_GREATER diff --git a/tests/MemoryPack.Tests/ArrayFormatterTest.cs b/tests/MemoryPack.Tests/ArrayFormatterTest.cs index b78125d3..4142b775 100644 --- a/tests/MemoryPack.Tests/ArrayFormatterTest.cs +++ b/tests/MemoryPack.Tests/ArrayFormatterTest.cs @@ -35,7 +35,7 @@ public void ArrayTes() Convert(xs).ToArray().Should().Equal(xs.ToArray()); } { - var xs =new ReadOnlyMemory(new int[] { 1, 10, 100 }); + var xs = new ReadOnlyMemory(new int[] { 1, 10, 100 }); Convert(xs).ToArray().Should().Equal(xs.ToArray()); } //{ @@ -53,6 +53,15 @@ public void ArrayTes() } } + [Fact] + public void CharArrayTest() + { + var input = new[] { 'a', 'b', 'c' }; + var bytes = MemoryPackSerializer.Serialize(input); + var output = MemoryPackSerializer.Deserialize(bytes)!; + Assert.True(input.SequenceEqual(output)); + } + [Theory] [InlineData(100, 100, 10, 5)] [InlineData(10, 20, 15, 5)]