{"record":{"id":"04a76be8710ab5c6","repo":"devlooped/moq","slug":"member-0-is-not-supported-for-protected-mocking","errorCode":null,"errorMessage":"Member {0} is not supported for protected mocking.","messagePattern":"Member (.+?) is not supported for protected mocking\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Moq/Protected/ProtectedMock.cs","lineNumber":495,"sourceCode":"                }\n                else if (ItRefAnyField(expr) is FieldInfo itRefAnyField)\n                {\n                    types[index] = itRefAnyField.FieldType.MakeByRefType();\n                }\n                else if (expr.NodeType == ExpressionType.MemberAccess)\n                {\n                    var member = (MemberExpression)expr;\n                    if (member.Member is FieldInfo field)\n                    {\n                        types[index] = field.FieldType;\n                    }\n                    else if (member.Member is PropertyInfo property)\n                    {\n                        types[index] = property.PropertyType;\n                    }\n                    else\n                    {\n                        throw new NotSupportedException(string.Format(\n                            Resources.Culture,\n                            Resources.UnsupportedMember,\n                            member.Member.Name));\n                    }\n                }\n                else\n                {\n                    types[index] = (expr.PartialEval() as ConstantExpression)?.Type;\n                }\n            }\n\n            return types;\n        }\n\n        static bool IsItRefAny(Expression expression)\n        {\n            return ItRefAnyField(expression) != null;\n        }","sourceCodeStart":477,"sourceCodeEnd":513,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/Protected/ProtectedMock.cs#L477-L513","documentation":"When converting argument-match expressions to types, Moq only understands MemberExpression members that are methods (for the parameter types) or properties (for their property type). Any other member kind (e.g. fields, events, indexers) used in a matcher is not supported for protected mocking and triggers this NotSupportedException.","triggerScenarios":"Using an ItExpr.Is<T>(x => x.SomeField ...) style matcher whose expression references a field or other non-method/non-property member, causing member.Member to be neither MethodInfo nor PropertyInfo.","commonSituations":"Matching on a public field in a predicate; porting a regular Moq It.Is matcher to the Protected/ItExpr API where fewer expression shapes are supported.","solutions":["Rewrite the matcher so it references a property or method, or use a simple constant/ItExpr matcher instead","Compute the value into a local variable before the setup and match with ItExpr.Is<T>(v => v == local) without member access","Use a plain value or ItExpr.IsAny<T>() if member-specific matching is unnecessary"],"exampleFix":"// before\nItExpr.Is<Customer>(c => c.Name == \"x\") // Name is a field\n// after\nItExpr.Is<Customer>(c => c.GetDisplayName() == \"x\") // method member, or make Name a property","handlingStrategy":"validation","validationCode":"// ensure matcher member access targets properties/methods, not fields\nclass Customer { public string Name => _name; } // property, not public field","typeGuard":null,"tryCatchPattern":"try { /* protected setup with member matcher */ } catch (NotSupportedException ex) { /* rewrite matcher with property/method or local constant */ }","preventionTips":["Avoid field access inside ItExpr.Is predicates","Match on pre-computed locals instead of member expressions","Keep protected-mock matchers simple: constants, ItExpr.IsAny, ItExpr.IsNull"],"tags":["moq","mocking","not-supported","expression"],"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"}