bchavez/Bogus · error · ArgumentException
Expression was not of the form 'x => x.Property or x => x.Fi
Error message
Expression was not of the form 'x => x.Property or x => x.Field'.
What it means
Thrown by PropertyName.GetMemberName when the expression body cannot be reduced to a MemberExpression (after unwrapping a UnaryExpression Convert). This means the lambda is not of the form 'x => x.Property' or 'x => x.Field' - e.g. a method call, arithmetic, or a naked parameter.
Source
Thrown at Source/Bogus/PropertyName.cs:55
$"that define how 'Foo' is generated. " +
"See this GitHub issue for more info: https://github.com/bchavez/Bogus/issues/115");
}
MemberExpression memberExpression;
if( expression is UnaryExpression unary )
//In this case the return type of the property was not object,
//so .Net wrapped the expression inside of a unary Convert()
//expression that casts it to type object. In this case, the
//Operand of the Convert expression has the original expression.
memberExpression = unary.Operand as MemberExpression;
else
//when the property is of type object the body itself is the
//correct expression
memberExpression = expression as MemberExpression;
if( memberExpression == null )
throw new ArgumentException(
"Expression was not of the form 'x => x.Property or x => x.Field'.");
return memberExpression.Member.Name;
}
}View on GitHub (pinned to 6ece18c5c2)
Solutions
- Ensure the lambda is strictly x => x.Property or x => x.Field referencing a real member.
- If you need a computed value, use the overload that accepts a Func<Faker,T,object> setter rather than an expression for the member.
- Bind to an actual field/property and compute the value in the setter delegate.
Example fix
// before f.RuleFor(x => x.CalculateTotal(), _ => 0); // after f.RuleFor(x => x.Total, _ => CalculateTotal());
Defensive patterns
Strategy: type-guard
Validate before calling
static bool IsMemberAccess<T,P>(Expression<Func<T,P>> e) {
var body = e.Body is UnaryExpression u ? u.Operand : e.Body;
return body is MemberExpression;
} Type guard
static string MemberOrThrow<T,P>(Expression<Func<T,P>> e) {
var body = e.Body is UnaryExpression u ? u.Operand : e.Body;
if (body is MemberExpression me) return me.Member.Name;
throw new InvalidOperationException("expression must be x => x.Member");
} Try / catch
try { f.RuleFor(expr, setter); }
catch (ArgumentException ex) when (ex.Message.Contains("not of the form")) {
// rewrite the lambda as a direct member selector
} Prevention
- Restrict RuleFor selectors to direct property/field access.
- Unit-test that each selector reduces to a MemberExpression.
- Avoid method calls or operators in member lambdas.
When it happens
Trigger: Calling RuleFor(x => x.GetHashCode(), ...) or RuleFor(x => x.Value + 1, ...) or passing a parameterless lambda. Any expression whose body is not a direct member access.
Common situations: Passing a computed expression instead of a property selector; refactoring a property into a method and forgetting to update the lambda.
Related errors
- Your expression '{expressionString}' cant be used. Nested ac
- {nameof(TType)} must be the same type as parameter named '{n
- Cannot create a rule set within a rule set.
- alphabet must contain at least 4 unique characters.
- A premium license is required to use this API. Please double
AI-assisted analysis of bchavez/Bogus@6ece18c5c2 (2026-08-13).
Data as JSON: /api/errors/10c6687b229a7193.
Report an issue: GitHub.