EllanJiang/GameFramework · error · GameFrameworkException

Try get value callback is invalid.

Error message

Try get value callback is invalid.

What it means

GameFrameworkSerializer<T> supports optional 'try get value' callbacks used during deserialization helpers. RegisterTryGetValueCallback throws when the callback parameter is null, since registering nothing would leave a broken entry in the callback table.

Solutions

  1. Pass a valid non-null TryGetValueCallback delegate for the version.
  2. Verify the resolved delegate is non-null before calling RegisterTryGetValueCallback.
  3. Fix the factory/reflection lookup that produced the null callback.
  4. Only register this optional callback when you actually have an implementation.

Example fix

// before
serializer.RegisterTryGetValueCallback(1, null);
// after
serializer.RegisterTryGetValueCallback(1, (Stream stream, out object value) =>
{
    value = reader.ReadInt32();
    return true;
});
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: Calling serializer.RegisterTryGetValueCallback(version, null), commonly because the delegate expression evaluated to null (missing method, failed factory, config-driven lookup).

Common situations: Refactoring removed the TryGetValue handler while registration remained; dynamic registration from reflection where the method wasn't found; copy-paste of register blocks with a placeholder null.

Related errors


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

Appendix: source

Thrown at GameFramework/Base/GameFrameworkSerializer.cs:105

                throw new GameFrameworkException("Deserialize callback is invalid.");
            }

            m_DeserializeCallbacks[version] = callback;
        }

        /// <summary>
        /// 注册尝试从指定流获取指定键的值回调函数。
        /// </summary>
        /// <param name="version">尝试从指定流获取指定键的值回调函数的版本。</param>
        /// <param name="callback">尝试从指定流获取指定键的值回调函数。</param>
        public void RegisterTryGetValueCallback(byte version, TryGetValueCallback callback)
        {
            if (callback == null)
            {
                throw new GameFrameworkException("Try get value callback is invalid.");
            }

            m_TryGetValueCallbacks[version] = callback;
        }

        /// <summary>
        /// 序列化数据到目标流中。
        /// </summary>
        /// <param name="stream">目标流。</param>
        /// <param name="data">要序列化的数据。</param>
        /// <returns>是否序列化数据成功。</returns>
        public bool Serialize(Stream stream, T data)
        {
            if (m_SerializeCallbacks.Count <= 0)
            {
                throw new GameFrameworkException("No serialize callback registered.");
            }

            return Serialize(stream, data, m_LatestSerializeCallbackVersion);
        }

View on GitHub (pinned to d0c010b051)