{"record":{"id":"c5bf1234d799fa2e","repo":"XINCGer/Unity3DTraining","slug":"messagetype-is-only-valid-for-message-fields","errorCode":null,"errorMessage":"MessageType is only valid for message fields.","messagePattern":"MessageType is only valid for message fields\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/Reflection/FieldDescriptor.cs","lineNumber":288,"sourceCode":"            {\n                if (fieldType != FieldType.Enum)\n                {\n                    throw new InvalidOperationException(\"EnumType is only valid for enum fields.\");\n                }\n                return enumType;\n            }\n        }\n\n        /// <summary>\n        /// For embedded message and group fields, returns the field's type.\n        /// </summary>\n        public MessageDescriptor MessageType\n        {\n            get\n            {\n                if (fieldType != FieldType.Message)\n                {\n                    throw new InvalidOperationException(\"MessageType is only valid for message fields.\");\n                }\n                return messageType;\n            }\n        }\n\n        /// <summary>\n        /// Look up and cross-link all field types etc.\n        /// </summary>\n        internal void CrossLink()\n        {\n            if (Proto.TypeName != \"\")\n            {\n                IDescriptor typeDescriptor =\n                    File.DescriptorPool.LookupSymbol(Proto.TypeName, this);\n\n                if (Proto.Type != 0)\n                {\n                    // Choose field type based on symbol.","sourceCodeStart":270,"sourceCodeEnd":306,"githubUrl":"https://github.com/XINCGer/Unity3DTraining/blob/016f98412ea5e11756b286736f479b66ab6216d5/NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/Reflection/FieldDescriptor.cs#L270-L306","documentation":"The FieldDescriptor.MessageType property only holds a value when the field's type is FieldType.Message. Reading it on any other field throws InvalidOperationException. Non-message fields have no nested message descriptor.","triggerScenarios":"Accessing descriptor.MessageType on a field whose FieldType is primitive, string, bytes, or enum — commonly in reflection code that walks fields assuming every field references a message.","commonSituations":"Generic serialization/diff tools that dereference MessageType for every field; groups/enum fields mishandled as messages.","solutions":["Check field.FieldType == FieldType.Message before accessing MessageType.","Restrict MessageType access to the message branch of your field-type handling code.","For fields that may be either, branch on FieldType before accessing either descriptor property."],"exampleFix":"// before\nvar messageType = field.MessageType;\n// after\nvar messageType = field.FieldType == FieldType.Message ? field.MessageType : null;","handlingStrategy":"type-guard","validationCode":"var messageType = field.FieldType == FieldType.Message ? field.MessageType : null;","typeGuard":"bool TryGetMessageType(FieldDescriptor f, out MessageDescriptor md)\n{ md = f.FieldType == FieldType.Message ? f.MessageType : null; return md != null; }","tryCatchPattern":"MessageDescriptor md = null;\ntry { md = field.MessageType; }\ncatch (InvalidOperationException) { /* not a message field */ }","preventionTips":["Check FieldType == FieldType.Message before dereferencing MessageType.","Never assume every field of a message contains a nested message.","Use non-throwing helpers in generic reflection code."],"tags":["protobuf","reflection","invalid-operation"],"backgroundTag":"invalid-state-transition","analyzedSha":"016f98412ea5e11756b286736f479b66ab6216d5","analyzedAt":"2026-09-12T01:08:58.711Z","contentChangedAt":"2026-09-12T01:08:58.711Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}