XINCGer/Unity3DTraining · error · InvalidOperationException

SetValue is not implemented for repeated fields

Error message

SetValue is not implemented for repeated fields

What it means

RepeatedFieldAccessor.SetValue is intentionally unimplemented: repeated fields are mutated through the IList returned by GetValue (Add/Clear/remove items), not by assigning a whole list. Calling SetValue on a repeated field's accessor throws InvalidOperationException by design.

Solutions

  1. For repeated fields, call GetValue(message) to get the IList and mutate it (Clear() then AddRange of source items).
  2. Check field.IsRepeated (or IsMap) before choosing between SetValue and list mutation.
  3. Use MessageDescriptor/IMessage reflection helpers or FieldCodec-based copying instead of raw SetValue.

Example fix

// before
accessor.SetValue(msg, otherList); // throws for repeated fields
// after
if (field.IsRepeated) {
    var list = (IList)accessor.GetValue(msg);
    list.Clear();
    foreach (var item in (IList)accessor.GetValue(other)) list.Add(item);
} else {
    accessor.SetValue(msg, accessor.GetValue(other));
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (field.IsRepeated) { /* mutate list */ } else { accessor.SetValue(msg, value); }

Type guard

bool CanSetValue(FieldDescriptor f) => !f.IsRepeated && !f.IsMap;

Try / catch

try { accessor.SetValue(msg, value); } catch (InvalidOperationException ex) when (ex.Message.Contains("repeated")) { var list = (IList)accessor.GetValue(msg); list.Clear(); foreach (var i in (IList)value) list.Add(i); }

Prevention

When it happens

Trigger: Using reflection-based field assignment (accessor.SetValue(message, list)) on a FieldDescriptor whose IsRepeated is true, e.g. generic serialization/copy code that treats all fields uniformly.

Common situations: Generic message-copy or data-binding frameworks that call SetValue for every field without checking IsRepeated/IsMap.

Related errors


AI-assisted analysis of XINCGer/Unity3DTraining@016f98412e (2026-09-12). Data as JSON: /api/errors/11cae64aef067da6. Report an issue: GitHub.

Appendix: source

Thrown at NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/Reflection/RepeatedFieldAccessor.cs:56

{
    /// <summary>
    /// Accessor for repeated fields.
    /// </summary>
    internal sealed class RepeatedFieldAccessor : FieldAccessorBase
    {
        internal RepeatedFieldAccessor(PropertyInfo property, FieldDescriptor descriptor) : base(property, descriptor)
        {
        }

        public override void Clear(IMessage message)
        {
            IList list = (IList) GetValue(message);
            list.Clear();
        }

        public override void SetValue(IMessage message, object value)
        {
            throw new InvalidOperationException("SetValue is not implemented for repeated fields");
        }

    }
}

View on GitHub (pinned to 016f98412e)