XINCGer/Unity3DTraining · error · InvalidOperationException
EnumType is only valid for enum fields.
Error message
EnumType is only valid for enum fields.
What it means
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.
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.
Example fix
// before var enumType = field.EnumType; // after var enumType = field.FieldType == FieldType.Enum ? field.EnumType : null;
Defensive patterns
Strategy: type-guard
Validate before calling
var enumType = field.FieldType == FieldType.Enum ? field.EnumType : null;
Type guard
bool TryGetEnumType(FieldDescriptor f, out EnumDescriptor ed)
{ ed = f.FieldType == FieldType.Enum ? f.EnumType : null; return ed != null; } Try / catch
EnumDescriptor ed = null;
try { ed = field.EnumType; }
catch (InvalidOperationException) { /* not an enum field */ } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Generic reflection loops over message fields that access EnumType unconditionally; code copied from a field that happened to be an enum.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- MessageType is only valid for message fields.
- SkipLastField cannot be called at the end of a stream
- SpaceLeft can only be called on CodedOutputStreams that are…
- Unexpected token type:
- FieldDescriptors can only be compared to other…
AI-assisted analysis of XINCGer/Unity3DTraining@016f98412e (2026-09-12).
Data as JSON: /api/errors/02acda4d58f39d5c.
Report an issue: GitHub.
Appendix: source
Thrown at NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/Reflection/FieldDescriptor.cs:273
{
if (other.ContainingType != ContainingType)
{
throw new ArgumentException("FieldDescriptors can only be compared to other FieldDescriptors " +
"for fields of the same message type.");
}
return FieldNumber - other.FieldNumber;
}
/// <summary>
/// For enum fields, returns the field's type.
/// </summary>
public EnumDescriptor EnumType
{
get
{
if (fieldType != FieldType.Enum)
{
throw new InvalidOperationException("EnumType is only valid for enum fields.");
}
return enumType;
}
}
/// <summary>
/// For embedded message and group fields, returns the field's type.
/// </summary>
public MessageDescriptor MessageType
{
get
{
if (fieldType != FieldType.Message)
{
throw new InvalidOperationException("MessageType is only valid for message fields.");
}
return messageType;
}View on GitHub (pinned to 016f98412e)