{"record":{"id":"02acda4d58f39d5c","repo":"XINCGer/Unity3DTraining","slug":"enumtype-is-only-valid-for-enum-fields","errorCode":null,"errorMessage":"EnumType is only valid for enum fields.","messagePattern":"EnumType is only valid for enum fields\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/Reflection/FieldDescriptor.cs","lineNumber":273,"sourceCode":"        {\n            if (other.ContainingType != ContainingType)\n            {\n                throw new ArgumentException(\"FieldDescriptors can only be compared to other FieldDescriptors \" +\n                                            \"for fields of the same message type.\");\n            }\n            return FieldNumber - other.FieldNumber;\n        }\n\n        /// <summary>\n        /// For enum fields, returns the field's type.\n        /// </summary>\n        public EnumDescriptor EnumType\n        {\n            get\n            {\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            }","sourceCodeStart":255,"sourceCodeEnd":291,"githubUrl":"https://github.com/XINCGer/Unity3DTraining/blob/016f98412ea5e11756b286736f479b66ab6216d5/NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/Reflection/FieldDescriptor.cs#L255-L291","documentation":"The FieldDescriptor.EnumType property only holds a value when the field's type is FieldType.Enum. Reading it on any other field throws InvalidOperationException. This is a misuse guard: the enum descriptor simply does not exist for non-enum fields.","triggerScenarios":"Accessing descriptor.EnumType on a field whose FieldType is a primitive, string, bytes, or message, e.g. reflection code that assumes every field has both MessageType and EnumType.","commonSituations":"Generic reflection loops over message fields that access EnumType unconditionally; code copied from a field that happened to be an enum.","solutions":["Check field.FieldType == FieldType.Enum before accessing EnumType.","Access it inside the enum branch of your field-type switch in reflection code.","If the field may be either, use pattern checks (fieldType switch) rather than direct property access."],"exampleFix":"// before\nvar enumType = field.EnumType;\n// after\nvar enumType = field.FieldType == FieldType.Enum ? field.EnumType : null;","handlingStrategy":"type-guard","validationCode":"var enumType = field.FieldType == FieldType.Enum ? field.EnumType : null;","typeGuard":"bool TryGetEnumType(FieldDescriptor f, out EnumDescriptor ed)\n{ ed = f.FieldType == FieldType.Enum ? f.EnumType : null; return ed != null; }","tryCatchPattern":"EnumDescriptor ed = null;\ntry { ed = field.EnumType; }\ncatch (InvalidOperationException) { /* not an enum field */ }","preventionTips":["Always branch reflection logic on FieldType before touching type-specific properties.","Handle both message and enum branches in field-walking utilities.","Write a helper accessor that returns null instead of throwing."],"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"}