{"record":{"id":"96a38ee41819364b","repo":"EllanJiang/GameFramework","slug":"type-is-invalid","errorCode":null,"errorMessage":"Type is invalid.","messagePattern":"Type is invalid\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Base/DataStruct/TypeNamePair.cs","lineNumber":40,"sourceCode":"        /// <summary>\n        /// 初始化类型和名称的组合值的新实例。\n        /// </summary>\n        /// <param name=\"type\">类型。</param>\n        public TypeNamePair(Type type)\n            : this(type, string.Empty)\n        {\n        }\n\n        /// <summary>\n        /// 初始化类型和名称的组合值的新实例。\n        /// </summary>\n        /// <param name=\"type\">类型。</param>\n        /// <param name=\"name\">名称。</param>\n        public TypeNamePair(Type type, string name)\n        {\n            if (type == null)\n            {\n                throw new GameFrameworkException(\"Type is invalid.\");\n            }\n\n            m_Type = type;\n            m_Name = name ?? string.Empty;\n        }\n\n        /// <summary>\n        /// 获取类型。\n        /// </summary>\n        public Type Type\n        {\n            get\n            {\n                return m_Type;\n            }\n        }\n\n        /// <summary>","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Base/DataStruct/TypeNamePair.cs#L22-L58","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nType t = Assembly.GetType(\"MyApp.Config\"); // null if misspelled\nvar key = new TypeNamePair(t, \"main\");\n// after\nType t = Assembly.GetType(\"MyApp.Config\") ?? throw new InvalidOperationException(\"Type not found\");\nvar key = new TypeNamePair(t, \"main\");","handlingStrategy":"validation","validationCode":"if (type == null) throw new ArgumentNullException(nameof(type));\nvar pair = new TypeNamePair(type, name);","typeGuard":"bool IsValidType(Type type) => type != null;","tryCatchPattern":"try\n{\n    var pair = new TypeNamePair(type, name);\n}\ncatch (GameFrameworkException ex) when (ex.Message == \"Type is invalid.\")\n{\n    // the reflected Type failed to resolve; check the type name/assembly\n}","preventionTips":["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"],"tags":["csharp","null-argument","gameframework","typenamepair"],"backgroundTag":"null-argument","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}