bchavez/Bogus · error · ArgumentException
The specified property '{property.Name}' does not have a set
Error message
The specified property '{property.Name}' does not have a setter method. What it means
ExtensionsForPropertyInfo.CreateSetter builds a delegate for a property's setter via reflection. If property.GetSetMethod(true) returns null - meaning the property is read-only (no set accessor at all, not even a non-public one) - it throws ArgumentException. The method is used internally by Faker<T> to wire RuleFor values onto properties.
Source
Thrown at Source/Bogus/Extensions/ExtensionsForPropertyInfo.cs:16
using System;
using System.Reflection;
namespace Bogus.Extensions;
public static class ExtensionsForPropertyInfo
{
private static readonly MethodInfo GenericSetterCreationMethod =
typeof(ExtensionsForPropertyInfo).GetMethod(nameof(CreateSetterGeneric), BindingFlags.Static | BindingFlags.NonPublic);
public static Action<T, object> CreateSetter<T>(this PropertyInfo property)
{
if (property == null) throw new ArgumentNullException(nameof(property));
var setter = property.GetSetMethod(true);
if (setter == null) throw new ArgumentException($"The specified property '{property.Name}' does not have a setter method.");
var genericHelper = GenericSetterCreationMethod.MakeGenericMethod(property.DeclaringType, property.PropertyType);
return (Action<T, object>)genericHelper.Invoke(null, new object[] { setter });
}
private static Action<T, object> CreateSetterGeneric<T, V>(MethodInfo setter) where T : class
{
var setterTypedDelegate =
#if STANDARD
(Action<T, V>) setter.CreateDelegate(typeof(Action<T, V>))
#else
(Action<T, V>) Delegate.CreateDelegate(typeof(Action<T, V>), setter)
#endif
;
var setterDelegate = (Action<T, object>)((T instance, object value) => { setterTypedDelegate(instance, (V)value); });
return setterDelegate;
}
View on GitHub (pinned to 6ece18c5c2)
Solutions
- Only declare RuleFor for properties that have a set accessor.
- Add a setter to the target property (public or non-public - GetSetMethod(true) looks for non-public too).
- Use RuleFor with a constructor/init path, or ignore the read-only property via FinishWith/omission.
Example fix
// before
public class Model { public string Name { get; } }
f.RuleFor(x => x.Name, _ => "x");
// after
public class Model { public string Name { get; set; } }
f.RuleFor(x => x.Name, _ => "x"); Defensive patterns
Strategy: validation
Validate before calling
var setter = property.GetSetMethod(true);
if (setter is null)
throw new InvalidOperationException($"{property.Name} has no setter; cannot map via RuleFor.");
// only register RuleFor for settable properties Type guard
static bool HasSetter(System.Reflection.PropertyInfo p) => p.GetSetMethod(true) is not null;
Prevention
- Only target RuleFor at properties with a set accessor.
- Filter mapped properties with HasSetter before configuring the Faker.
- Prefer mutable DTOs/POCOs as Faker targets over records with get-only properties.
When it happens
Trigger: A RuleFor targeting a get-only property (e.g. `public string X { get; }` or `public string X => ...`) whose backing has no setter, when Faker tries to create a setter delegate for it.
Common situations: Mapping a Faker<T> onto a class with computed or init-only-without-setter properties, or onto a record with a get-only property that the generator tries to populate.
Related errors
- {nameof(amountToPick)} needs to be a positive integer.
- {nameof(amountToPick)} is greater than the number of items.
- .{nameof(OrNull)}() {nameof(nullWeight)} of '{nullWeight}' m
- .{nameof(OrDefault)}() {nameof(defaultWeight)} of '{defaultW
- The locale '{locale}' does not exist. To see all available l
AI-assisted analysis of bchavez/Bogus@6ece18c5c2 (2026-08-13).
Data as JSON: /api/errors/ab9d407c9899f087.
Report an issue: GitHub.