{"record":{"id":"dd575fdab4e1f03f","repo":"devlooped/moq","slug":"argumentnullexception-value-cannot-be-null-parameter-type","errorCode":null,"errorMessage":"ArgumentNullException: Value cannot be null. (Parameter 'type')","messagePattern":"ArgumentNullException: Value cannot be null\\. \\(Parameter 'type'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Moq/TypeMatcherAttribute.cs","lineNumber":42,"sourceCode":"        ///     Use this constructor overload if the type on which this attribute is placed implements <see cref=\"ITypeMatcher\"/> itself.\n        ///   </para>\n        /// </summary>\n        public TypeMatcherAttribute()\n        {\n            this.type = null;\n        }\n\n        /// <summary>\n        ///   Initializes a new instance of the <see cref=\"TypeMatcherAttribute\"/> class.\n        ///   <para>\n        ///     Use this constructor overload if the type on which this attribute is placed does not implement <see cref=\"ITypeMatcher\"/>.\n        ///     The specified type will instead provide the implementation of <see cref=\"ITypeMatcher\"/>.\n        ///   </para>\n        /// </summary>\n        /// <param name=\"type\">The <see cref=\"Type\"/> of a type that implements <see cref=\"ITypeMatcher\"/>.</param>\n        public TypeMatcherAttribute(Type type)\n        {\n            this.type = type ?? throw new ArgumentNullException(nameof(type));\n        }\n\n        internal Type? Type => this.type;\n    }\n}\n","sourceCodeStart":24,"sourceCodeEnd":48,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/TypeMatcherAttribute.cs#L24-L48","documentation":"This ArgumentNullException is thrown by the TypeMatcherAttribute constructor when the 'type' parameter is null. Moq requires a concrete Type that implements ITypeMatcher to build the type matcher, so a null argument cannot produce a usable attribute and is rejected immediately at construction time.","triggerScenarios":"Calling new TypeMatcherAttribute(null) directly, or via reflection/attribute instantiation where typeof(...) resolves to null, e.g. dynamic scenarios: attribute = new TypeMatcherAttribute(FindType(name)) where FindType returns null.","commonSituations":"Reflection-based code resolving a matcher type by name with Type.GetType returning null (wrong assembly-qualified name or missing assembly); refactoring that renamed/deleted the matcher type; code that conditionally passes a type which is null in some build configurations.","solutions":["Pass a non-null System.Type, e.g. new TypeMatcherAttribute(typeof(MyMatcher))","If resolving by name, verify Type.GetType/name resolution succeeded before constructing the attribute; check assembly-qualified names","Ensure the referenced assembly containing the ITypeMatcher implementation is loaded and present at runtime","Add a null check or validation on the source value before passing it to the constructor"],"exampleFix":"// before\nvar attr = new TypeMatcherAttribute(Type.GetType(\"MyApp.MyMatcher, MyApp\"));\n// after\nvar matcherType = Type.GetType(\"MyApp.MyMatcher, MyApp\") ?? throw new InvalidOperationException(\"Matcher type 'MyApp.MyMatcher' not found; check assembly reference.\");\nvar attr = new TypeMatcherAttribute(matcherType);","handlingStrategy":"validation","validationCode":"if (matcherType is null)\n    throw new InvalidOperationException(\"Type matcher type could not be resolved; ensure the assembly is referenced and the type name is correct.\");\nvar attr = new TypeMatcherAttribute(matcherType);","typeGuard":"static bool IsValidMatcherType([NotNullWhen(true)] Type? type) =>\n    type is not null && typeof(ITypeMatcher).IsAssignableFrom(type);","tryCatchPattern":"try\n{\n    var attr = new TypeMatcherAttribute(matcherType);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"type\")\n{\n    // matcherType was null: fix type resolution before retrying\n}","preventionTips":["Always use typeof(T) rather than name-based type resolution when possible","When resolving types by string, assert the result is non-null immediately after Type.GetType","Keep matcher types in referenced assemblies so runtime lookup cannot fail silently","Wrap dynamic attribute construction in a helper that validates inputs first"],"tags":["csharp","null-argument","reflection","moq","attribute-construction"],"backgroundTag":"null-argument","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"}