{"record":{"id":"0f117bbf025f9ed7","repo":"devlooped/moq","slug":"expression-0-involves-a-field-access-which-is-not-supported","errorCode":null,"errorMessage":"Expression {0} involves a field access, which is not supported. Use properties instead.","messagePattern":"Expression (.+?) involves a field access, which is not supported\\. Use properties instead\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Moq/Guard.cs","lineNumber":202,"sourceCode":"        /// <see cref=\"ArgumentException\"/> in the latter.\n        /// </summary>\n        public static void NotNullOrEmpty(string value, string paramName)\n        {\n            if (value == null)\n            {\n                throw new ArgumentNullException(paramName);\n            }\n\n            if (value.Length == 0)\n            {\n                throw new ArgumentException(Resources.ArgumentCannotBeEmpty, paramName);\n            }\n        }\n\n        public static void NotField(MemberExpression memberAccess)\n        {\n            if (memberAccess.Member is FieldInfo)\n                throw new NotSupportedException(\n                    string.Format(\n                        Resources.FieldsNotSupported,\n                        memberAccess.ToStringFixed()));\n        }\n\n        public static void IsMockable(Type type)\n        {\n            if (!type.IsMockable())\n            {\n                throw new NotSupportedException(\n                    string.Format(\n                        Resources.TypeNotMockable,\n                        type.GetFormattedName()));\n            }\n        }\n\n        public static void Positive(TimeSpan delay)\n        {","sourceCodeStart":184,"sourceCodeEnd":220,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/Guard.cs#L184-L220","documentation":"Moq throws this NotSupportedException when a LINQ expression used in a setup (e.g. mock.Setup(...)) contains a field access instead of a property access. Moq matches setups via member expressions and only supports properties, methods, and indexers; CLR fields cannot be intercepted by the generated proxy.","triggerScenarios":"Calling mock.Setup(m => m.someField) or mock.Verify(m => m.someField) where someField is a public (or accessible) field on the mocked class; also passing a lambda body like m => m.field + 1 that reads a field inside a setup expression.","commonSituations":"Mocking legacy or DTO classes that expose public fields instead of properties; refactoring a property to a field; third-party/interop types with public fields; copy-pasting member names and accidentally picking a field.","solutions":["Replace the public field with a property on the mocked type (public int X => field; or an auto-property).","If you cannot change the type, set the field directly on the mock's real backing object (use Mock.Of / manual instantiation) instead of Setup, or wrap it behind an interface with properties.","Use SetupSet/VerifySet only on properties; for field state checks, assert on the field value after invoking the code under test rather than setting it up via Moq."],"exampleFix":"// before\nmock.Setup(m => m.Name).Returns(\"x\"); // Name is a public FIELD\n// after\nmock.Setup(m => m.Name).Returns(\"x\"); // after converting Name to a property\n// or if the class cannot change:\nvar real = new Customer { Name = \"x\" }; // set field directly, don't Setup it","handlingStrategy":"validation","validationCode":"static bool IsFieldAccess(LambdaExpression expr)\n{\n    var body = expr.Body as MemberExpression;\n    return body?.Member is System.Reflection.FieldInfo;\n}\n// before mock.Setup: if (IsFieldAccess(expr)) throw/fix\n","typeGuard":"static bool IsProperty(LambdaExpression expr) =>\n    (expr.Body as System.Linq.Expressions.MemberExpression)?.Member is System.Reflection.PropertyInfo;\n","tryCatchPattern":"try { mock.Setup(expr).Returns(value); }\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"field access\"))\n{\n    // convert the member to a property or set the field directly\n}\n","preventionTips":["Prefer properties over public fields in types designed for mocking","Use nameof()/expression-based navigation in IDEs that flag field vs property misuse","Review DTOs for public fields before mocking them","Wrap field-based third-party types behind property-based interfaces"],"tags":["moq","linq-expressions","field-access","unsupported-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"89a5be629c752960fe403e95ff583e4ae6f00542","analyzedAt":"2026-09-16T05:31:39.496Z","contentChangedAt":"2026-09-16T05:31:39.496Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}