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

  1. Only declare RuleFor for properties that have a set accessor.
  2. Add a setter to the target property (public or non-public - GetSetMethod(true) looks for non-public too).
  3. 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

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


AI-assisted analysis of bchavez/Bogus@6ece18c5c2 (2026-08-13). Data as JSON: /api/errors/ab9d407c9899f087. Report an issue: GitHub.