EllanJiang/GameFramework · error · GameFrameworkException

Serialize callback is invalid.

Error message

Serialize callback is invalid.

What it means

GameFrameworkSerializer<T> stores per-version serialize callbacks used when writing data to a stream. RegisterSerializeCallback requires an actual delegate; it throws when the callback parameter is null because a null entry would cause a NullReferenceException later during Serialize.

Solutions

  1. Pass a valid non-null SerializeCallback delegate (e.g. a static/instance method or lambda) for the version.
  2. Verify the delegate expression actually resolves (no null-returning factory) before registering.
  3. Ensure the object owning the callback method is alive in Unity (not destroyed) when registering.
  4. Guard registration with a null check on the callback and log/skip if null.

Example fix

// before
serializer.RegisterSerializeCallback(1, null);
// after
serializer.RegisterSerializeCallback(1, (stream, data) =>
{
    stream.Write(BitConverter.GetBytes(data.Id), 0, sizeof(int));
    return true;
});
Defensive patterns

Strategy: validation

Validate before calling

if (callback == null) { throw new InvalidOperationException("Serialize callback required."); }
serializer.RegisterSerializeCallback(version, callback);

Type guard

static bool IsValidCallback(GameFrameworkSerializer<T>.SerializeCallback cb) => cb != null;

Try / catch

try { serializer.RegisterSerializeCallback(version, callback); } catch (GameFrameworkException) { /* log null callback registration attempt */ }

Prevention

When it happens

Trigger: Calling serializer.RegisterSerializeCallback(version, null), often due to a typo'd method group, a conditional that resolved to null, or a delegate field that was never assigned.

Common situations: Passing a method name that doesn't exist/compile to a cast delegate, registering callbacks from config where the target object was destroyed (Unity), or copy-paste errors leaving one registration with a null delegate.

Related errors


AI-assisted analysis of EllanJiang/GameFramework@d0c010b051 (2026-09-15). Data as JSON: /api/errors/9db18d3231bb0096. Report an issue: GitHub.

Appendix: source

Thrown at GameFramework/Base/GameFrameworkSerializer.cs:68

        /// <summary>
        /// 尝试从指定流获取指定键的值回调函数。
        /// </summary>
        /// <param name="stream">指定流。</param>
        /// <param name="key">指定键。</param>
        /// <param name="value">指定键的值。</param>
        /// <returns>是否从指定流获取指定键的值成功。</returns>
        public delegate bool TryGetValueCallback(Stream stream, string key, out object value);

        /// <summary>
        /// 注册序列化回调函数。
        /// </summary>
        /// <param name="version">序列化回调函数的版本。</param>
        /// <param name="callback">序列化回调函数。</param>
        public void RegisterSerializeCallback(byte version, SerializeCallback callback)
        {
            if (callback == null)
            {
                throw new GameFrameworkException("Serialize callback is invalid.");
            }

            m_SerializeCallbacks[version] = callback;
            if (version > m_LatestSerializeCallbackVersion)
            {
                m_LatestSerializeCallbackVersion = version;
            }
        }

        /// <summary>
        /// 注册反序列化回调函数。
        /// </summary>
        /// <param name="version">反序列化回调函数的版本。</param>
        /// <param name="callback">反序列化回调函数。</param>
        public void RegisterDeserializeCallback(byte version, DeserializeCallback callback)
        {
            if (callback == null)
            {

View on GitHub (pinned to d0c010b051)