{"record":{"id":"11cae64aef067da6","repo":"XINCGer/Unity3DTraining","slug":"setvalue-is-not-implemented-for-repeated-fields","errorCode":null,"errorMessage":"SetValue is not implemented for repeated fields","messagePattern":"SetValue is not implemented for repeated fields","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/Reflection/RepeatedFieldAccessor.cs","lineNumber":56,"sourceCode":"{\n    /// <summary>\n    /// Accessor for repeated fields.\n    /// </summary>\n    internal sealed class RepeatedFieldAccessor : FieldAccessorBase\n    {\n        internal RepeatedFieldAccessor(PropertyInfo property, FieldDescriptor descriptor) : base(property, descriptor)\n        {\n        }\n\n        public override void Clear(IMessage message)\n        {\n            IList list = (IList) GetValue(message);\n            list.Clear();\n        }\n\n        public override void SetValue(IMessage message, object value)\n        {\n            throw new InvalidOperationException(\"SetValue is not implemented for repeated fields\");\n        }\n\n    }\n}\n","sourceCodeStart":38,"sourceCodeEnd":61,"githubUrl":"https://github.com/XINCGer/Unity3DTraining/blob/016f98412ea5e11756b286736f479b66ab6216d5/NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/Reflection/RepeatedFieldAccessor.cs#L38-L61","documentation":"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.","triggerScenarios":"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.","commonSituations":"Generic message-copy or data-binding frameworks that call SetValue for every field without checking IsRepeated/IsMap.","solutions":["For repeated fields, call GetValue(message) to get the IList and mutate it (Clear() then AddRange of source items).","Check field.IsRepeated (or IsMap) before choosing between SetValue and list mutation.","Use MessageDescriptor/IMessage reflection helpers or FieldCodec-based copying instead of raw SetValue."],"exampleFix":"// before\naccessor.SetValue(msg, otherList); // throws for repeated fields\n// after\nif (field.IsRepeated) {\n    var list = (IList)accessor.GetValue(msg);\n    list.Clear();\n    foreach (var item in (IList)accessor.GetValue(other)) list.Add(item);\n} else {\n    accessor.SetValue(msg, accessor.GetValue(other));\n}","handlingStrategy":"type-guard","validationCode":"if (field.IsRepeated) { /* mutate list */ } else { accessor.SetValue(msg, value); }","typeGuard":"bool CanSetValue(FieldDescriptor f) => !f.IsRepeated && !f.IsMap;","tryCatchPattern":"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); }","preventionTips":["Treat repeated fields as lists to mutate, never as settable values.","Centralize reflection-based copy logic in one helper that branches on IsRepeated/IsMap."],"tags":["protobuf","reflection","repeated-fields","unsupported-operation"],"backgroundTag":"method-not-implemented","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"}