{"record":{"id":"7dd0fca54230fdbd","repo":"XINCGer/Unity3DTraining","slug":"fielddescriptors-can-only-be-compared-to-other-fie","errorCode":null,"errorMessage":"FieldDescriptors can only be compared to other FieldDescriptors for fields of the same message type.","messagePattern":"FieldDescriptors can only be compared to other FieldDescriptors for fields of the same message type\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/Reflection/FieldDescriptor.cs","lineNumber":258,"sourceCode":"        public int FieldNumber\n        {\n            get\n            {\n                return Proto.Number;\n            }\n        }\n\n        /// <summary>\n        /// Compares this descriptor with another one, ordering in \"canonical\" order\n        /// which simply means ascending order by field number. <paramref name=\"other\"/>\n        /// must be a field of the same type, i.e. the <see cref=\"ContainingType\"/> of\n        /// both fields must be the same.\n        /// </summary>\n        public int CompareTo(FieldDescriptor other)\n        {\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            }","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/XINCGer/Unity3DTraining/blob/016f98412ea5e11756b286736f479b66ab6216d5/NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/Reflection/FieldDescriptor.cs#L240-L276","documentation":"FieldDescriptor.CompareTo orders fields by field number, but only fields of the SAME message type have a meaningful order. If the other descriptor belongs to a different containing type, it throws ArgumentException. It exists so fields can be sorted (e.g. for reflection-based serialization order).","triggerScenarios":"Calling fieldDescriptor1.CompareTo(fieldDescriptor2) where the two descriptors have different ContainingType references, e.g. sorting a mixed list built from two message types, or comparing a field of message A with one of message B.","commonSituations":"Reflection code that collects FieldDescriptors across multiple message types into one list and sorts it; dictionary/set comparisons relying on IComparable across message types.","solutions":["Group descriptors by ContainingType and only sort within one message type.","Use FieldNumber for ordering instead of CompareTo when comparing across types.","Guard the comparison with a ContainingType equality check before calling CompareTo."],"exampleFix":"// before\nlist.Sort((a, b) => a.CompareTo(b)); // mixed types\n// after\nlist.Sort((a, b) => a.ContainingType == b.ContainingType\n    ? a.FieldNumber.CompareTo(b.FieldNumber)\n    : string.CompareOrdinal(a.ContainingType.FullName, b.ContainingType.FullName));","handlingStrategy":"type-guard","validationCode":"if (a.ContainingType != b.ContainingType)\n    throw new InvalidOperationException(\"Cannot CompareTo fields of different message types\");","typeGuard":"bool SameMessage(a, b) => ReferenceEquals(a.ContainingType, b.ContainingType);","tryCatchPattern":"int cmp;\ntry { cmp = a.CompareTo(b); }\ncatch (ArgumentException) { cmp = string.CompareOrdinal(a.ContainingType.FullName, b.ContainingType.FullName); }","preventionTips":["Sort fields per message type, never in one global list.","Prefer comparing FieldNumber directly for cross-type ordering.","Group descriptors by ContainingType.FullName in reflection utilities."],"tags":["protobuf","reflection","comparison"],"backgroundTag":"invalid-argument-value","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"}