{"record":{"id":"f6f56d5e38e16491","repo":"stride3d/stride","slug":"could-not-find-serializer-for-type-typeof-t","errorCode":null,"errorMessage":"Could not find serializer for type {typeof(T)}.","messagePattern":"Could not find serializer for type (.+?)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core/Serialization/SerializerExtensions.cs","lineNumber":93,"sourceCode":"    [MethodImpl(MethodImplOptions.AggressiveInlining)]\n    public static void Write<T>(this SerializationStream stream, T obj)\n    {\n        Serialize(stream, ref obj, ArchiveMode.Serialize);\n    }\n\n    /// <summary>\n    /// Serializes the specified object.\n    /// </summary>\n    /// <typeparam name=\"T\"></typeparam>\n    /// <param name=\"stream\">The stream to serialize to.</param>\n    /// <param name=\"obj\">The object to serialize.</param>\n    /// <param name=\"mode\">The serialization mode.</param>\n    [MethodImpl(MethodImplOptions.AggressiveInlining)]\n    public static void Serialize<T>(this SerializationStream stream, ref T obj, ArchiveMode mode)\n    {\n        var dataSerializer = stream.Context.SerializerSelector.GetSerializer<T>();\n        if (dataSerializer == null)\n            throw new InvalidOperationException($\"Could not find serializer for type {typeof(T)}.\");\n\n        dataSerializer.PreSerialize(ref obj, mode, stream);\n        dataSerializer.Serialize(ref obj, mode, stream);\n    }\n\n    /// <summary>Serializes or deserializes the value using <see cref=\"SerializationStream.Serialize(Span{byte})\"/>.</summary>\n    public static void Serialize<T>(this SerializationStream serializer, ref T value)\n    {\n        ref var b = ref Unsafe.As<T, byte>(ref value);\n        var span = MemoryMarshal.CreateSpan(ref b, Unsafe.SizeOf<T>());\n        serializer.Serialize(span);\n    }\n\n    /// <summary>\n    /// Reads a boolean value from the stream.\n    /// </summary>\n    /// <param name=\"stream\">The stream.</param>\n    /// <returns>A boolean value read from the stream.</returns>","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core/Serialization/SerializerExtensions.cs#L75-L111","documentation":"This generic extension method on SerializationStream fetches the DataSerializer for T from the context's SerializerSelector and throws InvalidOperationException when none is registered. Unlike the internal code paths this is a public convenience API, so callers hitting it serialized a type that simply has no serializer available in the current serialization context.","triggerScenarios":"Calling stream.Serialize(ref myObj) (or Write) for a type T without a [DataContract]/generated or custom serializer; a custom SerializerSelector attached to the SerializationContext that excludes T's serializer; serializing before assembly registration of T's module; serializing unannotated third-party types.","commonSituations":"Quick custom binary save code writing component fields directly with stream.Serialize; serializing enums/structs from an assembly without generated serializers; swapping in a restricted SerializerSelector for partial serialization and forgetting the needed type; unit tests constructing a SerializationStream without the default selector.","solutions":["Ensure the type is [DataContract] (or has a [DataSerializer]) and rebuild so generated serializers are registered","Check stream.Context.SerializerSelector and use the default selector that includes the serializer for T","Register T's assembly via Serialization.RegisterSerializationAssembly before serializing","Write and register a custom DataSerializer for types you cannot annotate","Catch InvalidOperationException and log typeof(T) to identify which type lacks a serializer"],"exampleFix":"// before\nstream.Serialize(ref customThing); // InvalidOperationException: no serializer\n// after\npublic class CustomThingSerializer : DataSerializer<CustomThing>\n{ /* implement Serialize/Deserialize */ }\n// register it, or mark CustomThing [DataContract] and rebuild","handlingStrategy":"try-catch","validationCode":"var selector = stream.Context.SerializerSelector;\nif (selector.GetSerializer<T>() == null)\n    throw new InvalidOperationException($\"Before calling Serialize, register a serializer for {typeof(T)}\");","typeGuard":"static bool HasSerializerFor<T>(SerializationStream stream) =>\n    stream.Context.SerializerSelector.GetSerializer<T>() is not null;","tryCatchPattern":"try\n{\n    stream.Serialize(ref value);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Could not find serializer for type\"))\n{\n    logger.Error(ex, \"No serializer registered for {Type}; add [DataContract]/[DataSerializer] or fix the SerializerSelector\", typeof(T));\n    throw;\n}","preventionTips":["Call this extension only for types covered by [DataContract]/generated serializers","Verify the SerializationContext uses a SerializerSelector that includes your types","Register serializer assemblies at startup before any save/load","Add a smoke test that serializes every serializable model type"],"tags":["serialization","extension-method","missing-serializer","stride"],"backgroundTag":"class-not-found","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}