© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•11mo ago•
12 replies
Nolram

NativeAOT Serialization Troubles

I'm trying to serialize the following (in an ideally fast manner and binary format, but I am willing to compromise) using NativeAOT (this is a requirement!):
A class containing a custom SparseSet (2 arrays internally) implementation containing a set of classes deriving from a shared interface.

I'm currently using MemoryPack for this, however it is not working whatsoever, and the following error occurs:
Process terminated. Failed to create generic virtual method implementation

Declaring type: MemoryPack.Formatters.MemoryPackableFormatter`1<GameCore.Scenes.Scene>
Method name: Serialize
Instantiation:
  Argument 00000000: MemoryPack.Internal.ReusableLinkedArrayBufferWriter

   at System.RuntimeExceptionHelpers.FailFast(String, Exception, String, RhFailFastReason, IntPtr, IntPtr) + 0x249
   at Internal.Runtime.TypeLoader.TypeLoaderEnvironment.ResolveGenericVirtualMethodTarget(RuntimeTypeHandle, RuntimeMethodHandle) + 0x256
   at System.Runtime.TypeLoaderExports.<>c.<GVMLookupForSlotSlow>b__8_0(IntPtr, IntPtr, Object, IntPtr&) + 0x3e
   at System.Runtime.TypeLoaderExports.CacheMiss(IntPtr, IntPtr, RuntimeObjectFactory, Object) + 0x3c
   at System.Runtime.TypeLoaderExports.GVMLookupForSlotSlow(Object, RuntimeMethodHandle) + 0x68
   at System.Runtime.TypeLoaderExports.GVMLookupForSlot(Object, RuntimeMethodHandle) + 0x91
   at MemoryPack.MemoryPackWriter`1.WriteValue[T](T&) + 0x3d
   at MemoryPack.MemoryPackSerializer.Serialize[T](T&, MemoryPackSerializerOptions) + 0x342
   at Game.Main(String[] args) + 0xaa
Process terminated. Failed to create generic virtual method implementation

Declaring type: MemoryPack.Formatters.MemoryPackableFormatter`1<GameCore.Scenes.Scene>
Method name: Serialize
Instantiation:
  Argument 00000000: MemoryPack.Internal.ReusableLinkedArrayBufferWriter

   at System.RuntimeExceptionHelpers.FailFast(String, Exception, String, RhFailFastReason, IntPtr, IntPtr) + 0x249
   at Internal.Runtime.TypeLoader.TypeLoaderEnvironment.ResolveGenericVirtualMethodTarget(RuntimeTypeHandle, RuntimeMethodHandle) + 0x256
   at System.Runtime.TypeLoaderExports.<>c.<GVMLookupForSlotSlow>b__8_0(IntPtr, IntPtr, Object, IntPtr&) + 0x3e
   at System.Runtime.TypeLoaderExports.CacheMiss(IntPtr, IntPtr, RuntimeObjectFactory, Object) + 0x3c
   at System.Runtime.TypeLoaderExports.GVMLookupForSlotSlow(Object, RuntimeMethodHandle) + 0x68
   at System.Runtime.TypeLoaderExports.GVMLookupForSlot(Object, RuntimeMethodHandle) + 0x91
   at MemoryPack.MemoryPackWriter`1.WriteValue[T](T&) + 0x3d
   at MemoryPack.MemoryPackSerializer.Serialize[T](T&, MemoryPackSerializerOptions) + 0x342
   at Game.Main(String[] args) + 0xaa


I've attached a sample of this scenario reproducing the above error. It works fine when not using NativeAOT, but as I mentioned NAOT is a requirement for me.
If anyone has any ideas on how to get this to serialize at all - no matter using what library or whatever - please inform me!
Program.cs1.62KB
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

NativeAOT + emscripten
C#CC# / help
5mo ago
NativeAOT multiple Dlls
C#CC# / help
5w ago
✅ COM + NativeAOT Help
C#CC# / help
2y ago
Serialization/Deserialization
C#CC# / help
2y ago