{"record":{"id":"10c6687b229a7193","repo":"bchavez/Bogus","slug":"expression-was-not-of-the-form-x-x-property-or","errorCode":null,"errorMessage":"Expression was not of the form 'x => x.Property or x => x.Field'.","messagePattern":"Expression was not of the form 'x => x\\.Property or x => x\\.Field'\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Source/Bogus/PropertyName.cs","lineNumber":55,"sourceCode":"            $\"that define how 'Foo' is generated. \" +\n            \"See this GitHub issue for more info: https://github.com/bchavez/Bogus/issues/115\");\n      }\n\n      MemberExpression memberExpression;\n\n      if( expression is UnaryExpression unary )\n         //In this case the return type of the property was not object,\n         //so .Net wrapped the expression inside of a unary Convert()\n         //expression that casts it to type object. In this case, the\n         //Operand of the Convert expression has the original expression.\n         memberExpression = unary.Operand as MemberExpression;\n      else\n         //when the property is of type object the body itself is the\n         //correct expression\n         memberExpression = expression as MemberExpression;\n\n      if( memberExpression == null )\n         throw new ArgumentException(\n            \"Expression was not of the form 'x => x.Property or x => x.Field'.\");\n\n      return memberExpression.Member.Name;\n   }\n}","sourceCodeStart":37,"sourceCodeEnd":60,"githubUrl":"https://github.com/bchavez/Bogus/blob/6ece18c5c22648ab4606e45a0aaf2f07fc5259e4/Source/Bogus/PropertyName.cs#L37-L60","documentation":"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.","triggerScenarios":"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.","commonSituations":"Passing a computed expression instead of a property selector; refactoring a property into a method and forgetting to update the lambda.","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."],"exampleFix":"// before\nf.RuleFor(x => x.CalculateTotal(), _ => 0);\n// after\nf.RuleFor(x => x.Total, _ => CalculateTotal());","handlingStrategy":"type-guard","validationCode":"static bool IsMemberAccess<T,P>(Expression<Func<T,P>> e) {\n    var body = e.Body is UnaryExpression u ? u.Operand : e.Body;\n    return body is MemberExpression;\n}","typeGuard":"static string MemberOrThrow<T,P>(Expression<Func<T,P>> e) {\n    var body = e.Body is UnaryExpression u ? u.Operand : e.Body;\n    if (body is MemberExpression me) return me.Member.Name;\n    throw new InvalidOperationException(\"expression must be x => x.Member\");\n}","tryCatchPattern":"try { f.RuleFor(expr, setter); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"not of the form\")) {\n    // rewrite the lambda as a direct member selector\n}","preventionTips":["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."],"tags":["bogus","propertyname","expression","member-access"],"backgroundTag":null,"analyzedSha":"6ece18c5c22648ab4606e45a0aaf2f07fc5259e4","analyzedAt":"2026-08-13T21:08:15.463Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}