EllanJiang/GameFramework · error · GameFrameworkException

Object type is invalid.

Error message

Object type is invalid.

What it means

Utility.Json.ToObject(Type, string) validates that the target Type argument is not null before delegating to the helper. A null Type cannot identify what to deserialize into, so the library throws immediately with this explicit message.

Solutions

  1. Fix the string used to resolve the type (use AssemblyQualifiedName or a valid name in the loaded assembly)
  2. Check Type.GetType return value first; log a clear error if null before calling ToObject
  3. Preserve types from IL2CPP/linker stripping with [Preserve] or link.xml
  4. Verify configs reference types that still exist after refactors

Example fix

// before
Type t = Type.GetType(cfg.TypeName);
object o = Utility.Json.ToObject(t, json); // t null -> throws
// after
Type t = Type.GetType(cfg.TypeName) ?? typeof(DefaultConfig);
object o = Utility.Json.ToObject(t, json);
Defensive patterns

Strategy: validation

Validate before calling

Type t = string.IsNullOrEmpty(typeName) ? null : Type.GetType(typeName, throwOnError: false);
if (t == null) { Log.Error("Unknown type: {0}", typeName); return null; }

Type guard

bool IsValidTargetType(Type t) => t != null;

Prevention

When it happens

Trigger: Utility.Json.ToObject(null, json) — typically the Type came from Type.GetType(name) returning null because the type name string is misspelled, lacks its assembly qualifier, or the type was stripped from the build (IL2CPP/managed code stripping).

Common situations: Data-driven deserialization where type names are stored in config files; IL2CPP stripping removing types not statically referenced; renaming or moving classes without updating type-name strings in configs.

Related errors


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

Appendix: source

Thrown at GameFramework/Utility/Utility.Json.cs:100

                }
            }

            /// <summary>
            /// 将 JSON 字符串反序列化为对象。
            /// </summary>
            /// <param name="objectType">对象类型。</param>
            /// <param name="json">要反序列化的 JSON 字符串。</param>
            /// <returns>反序列化后的对象。</returns>
            public static object ToObject(Type objectType, string json)
            {
                if (s_JsonHelper == null)
                {
                    throw new GameFrameworkException("JSON helper is invalid.");
                }

                if (objectType == null)
                {
                    throw new GameFrameworkException("Object type is invalid.");
                }

                try
                {
                    return s_JsonHelper.ToObject(objectType, json);
                }
                catch (Exception exception)
                {
                    if (exception is GameFrameworkException)
                    {
                        throw;
                    }

                    throw new GameFrameworkException(Text.Format("Can not convert to object with exception '{0}'.", exception), exception);
                }
            }
        }
    }

View on GitHub (pinned to d0c010b051)