EllanJiang/GameFramework · error · GameFrameworkException
Type is invalid.
Error message
Type is invalid.
What it means
The TypeNamePair(Type type, string name) constructor throws GameFrameworkException('Type is invalid.') when the type argument is null. A TypeNamePair is a composite key of a type plus an optional name, and the type portion is mandatory — a null type would produce a broken key. Unlike name, which defaults to empty string, type gets no such fallback.
Solutions
- Pass a valid System.Type (e.g. typeof(MyClass)) to the TypeNamePair constructor
- If the type comes from reflection, check it for null before constructing and fix the type name/assembly reference if null
- Guard with `if (type == null) throw ...` in helper code that builds TypeNamePair instances from dynamic input
Example fix
// before
Type t = Assembly.GetType("MyApp.Config"); // null if misspelled
var key = new TypeNamePair(t, "main");
// after
Type t = Assembly.GetType("MyApp.Config") ?? throw new InvalidOperationException("Type not found");
var key = new TypeNamePair(t, "main"); Defensive patterns
Strategy: validation
Validate before calling
if (type == null) throw new ArgumentNullException(nameof(type)); var pair = new TypeNamePair(type, name);
Type guard
bool IsValidType(Type type) => type != null;
Try / catch
try
{
var pair = new TypeNamePair(type, name);
}
catch (GameFrameworkException ex) when (ex.Message == "Type is invalid.")
{
// the reflected Type failed to resolve; check the type name/assembly
} Prevention
- Prefer typeof(T) over string-based reflection when the type is known at compile time
- Check Assembly.GetType results for null before using them
- Never store Type fields without initializing them before key construction
When it happens
Trigger: Constructing new TypeNamePair(null, someName) directly, or via factory/lookup code that passes a System.Type obtained from reflection (GetType, typeof via variable, Assembly.GetType) that returned null because the type name was misspelled or the assembly is not loaded.
Common situations: Assembly.GetType("Wrong.Namespace.Type") returning null and being passed straight into the constructor; a variable holding a reflected Type that failed to resolve; storing Type in a field that was never initialized before building the key.
Related errors
- Owner is invalid.
- Resource manager is invalid.
- Data provider helper is invalid.
- Event is invalid.
- Data row type is invalid.
AI-assisted analysis of EllanJiang/GameFramework@d0c010b051 (2026-09-15).
Data as JSON: /api/errors/96a38ee41819364b.
Report an issue: GitHub.
Appendix: source
Thrown at GameFramework/Base/DataStruct/TypeNamePair.cs:40
/// <summary>
/// 初始化类型和名称的组合值的新实例。
/// </summary>
/// <param name="type">类型。</param>
public TypeNamePair(Type type)
: this(type, string.Empty)
{
}
/// <summary>
/// 初始化类型和名称的组合值的新实例。
/// </summary>
/// <param name="type">类型。</param>
/// <param name="name">名称。</param>
public TypeNamePair(Type type, string name)
{
if (type == null)
{
throw new GameFrameworkException("Type is invalid.");
}
m_Type = type;
m_Name = name ?? string.Empty;
}
/// <summary>
/// 获取类型。
/// </summary>
public Type Type
{
get
{
return m_Type;
}
}
/// <summary>View on GitHub (pinned to d0c010b051)