{"record":{"id":"52f62c9180386797","repo":"JamesNK/Newtonsoft.Json","slug":"error-setting-value-to-0-on-1-52f62c","errorCode":null,"errorMessage":"Error setting value to '{0}' on '{1}'.","messagePattern":"Error setting value to '(.+?)' on '(.+?)'\\.","errorType":"exception","errorClass":"JsonSerializationException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Serialization/ReflectionValueProvider.cs","lineNumber":63,"sourceCode":"        {\n            ValidationUtils.ArgumentNotNull(memberInfo, nameof(memberInfo));\n            _memberInfo = memberInfo;\n        }\n\n        /// <summary>\n        /// Sets the value.\n        /// </summary>\n        /// <param name=\"target\">The target to set the value on.</param>\n        /// <param name=\"value\">The value to set on the target.</param>\n        public void SetValue(object target, object? value)\n        {\n            try\n            {\n                ReflectionUtils.SetMemberValue(_memberInfo, target, value);\n            }\n            catch (Exception ex)\n            {\n                throw new JsonSerializationException(\"Error setting value to '{0}' on '{1}'.\".FormatWith(CultureInfo.InvariantCulture, _memberInfo.Name, target.GetType()), ex);\n            }\n        }\n\n        /// <summary>\n        /// Gets the value.\n        /// </summary>\n        /// <param name=\"target\">The target to get the value from.</param>\n        /// <returns>The value.</returns>\n        public object? GetValue(object target)\n        {\n            try\n            {\n                // https://github.com/dotnet/corefx/issues/26053\n                if (_memberInfo is PropertyInfo propertyInfo && propertyInfo.PropertyType.IsByRef)\n                {\n                    throw new InvalidOperationException(\"Could not create getter for {0}. ByRef return values are not supported.\".FormatWith(CultureInfo.InvariantCulture, propertyInfo));\n                }\n","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Serialization/ReflectionValueProvider.cs#L45-L81","documentation":"Thrown by ReflectionValueProvider.SetValue when ReflectionUtils.SetMemberValue throws while assigning a value to a member (field or property) of the target. '{0}' is the member name, '{1}' is the target runtime type; the original exception is the InnerException. ReflectionValueProvider is the fallback provider used when neither dynamic-method nor expression providers are available (e.g. limited trust / restricted reflection).","triggerScenarios":"Deserializing JSON into a member via plain reflection where SetMemberValue fails: type mismatch, setter throws ArgumentException, attempt to set a const/read-only/initializer-only field, value is incompatible with the member's underlying type.","commonSituations":"Running under partial trust or restricted reflection where DynamicValueProvider/ExpressionValueProvider are unavailable; setting a readonly field; assigning a derived type to a property whose setter validates the value; version where ReflectionValueProvider is the active provider.","solutions":["Inspect InnerException to identify the underlying setter/reflection failure.","Ensure the member has an accessible setter and the JSON value type is assignable to it.","Use a parameterized constructor/[JsonConstructor] to supply read-only members.","Register a custom JsonConverter to coerce the value to the correct type before assignment."],"exampleFix":"// before: readonly field cannot be set via reflection\npublic class Model { public readonly int Id; }\nJsonConvert.DeserializeObject<Model>(\"{\\\"Id\\\":5}\");\n// after: settable property or constructor\npublic class Model {\n    public Model(int id) { Id = id; }\n    public int Id { get; }\n}","handlingStrategy":"try-catch","validationCode":"var prop = target.GetType().GetProperty(memberName); if (prop != null && !prop.CanWrite) throw new InvalidOperationException(memberName + \" is read-only\");","typeGuard":null,"tryCatchPattern":"try { JsonConvert.DeserializeObject<T>(json); }\ncatch (JsonSerializationException ex) when (ex.Message.Contains(\"Error setting value\")) {\n    logger.Error(ex.InnerException, \"reflection setter failed at {Path}\", ex.Path); throw;\n}","preventionTips":["Ensure all serialized members have accessible setters.","Use [JsonConstructor] for read-only/init-only members.","Avoid readonly fields for deserialized data.","Register a custom JsonConverter for members needing coercion."],"tags":["deserialization","reflection","reflection-value-provider","setter"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}