devlooped/moq · error · ArgumentNullException

ArgumentNullException: Value cannot be null. (Parameter…

Error message

ArgumentNullException: Value cannot be null. (Parameter 'type')

What it means

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.

Solutions

  1. Pass a non-null System.Type, e.g. new TypeMatcherAttribute(typeof(MyMatcher))
  2. If resolving by name, verify Type.GetType/name resolution succeeded before constructing the attribute; check assembly-qualified names
  3. Ensure the referenced assembly containing the ITypeMatcher implementation is loaded and present at runtime
  4. Add a null check or validation on the source value before passing it to the constructor

Example fix

// before
var attr = new TypeMatcherAttribute(Type.GetType("MyApp.MyMatcher, MyApp"));
// after
var matcherType = Type.GetType("MyApp.MyMatcher, MyApp") ?? throw new InvalidOperationException("Matcher type 'MyApp.MyMatcher' not found; check assembly reference.");
var attr = new TypeMatcherAttribute(matcherType);
Defensive patterns

Strategy: validation

Validate before calling

if (matcherType is null)
    throw new InvalidOperationException("Type matcher type could not be resolved; ensure the assembly is referenced and the type name is correct.");
var attr = new TypeMatcherAttribute(matcherType);

Type guard

static bool IsValidMatcherType([NotNullWhen(true)] Type? type) =>
    type is not null && typeof(ITypeMatcher).IsAssignableFrom(type);

Try / catch

try
{
    var attr = new TypeMatcherAttribute(matcherType);
}
catch (ArgumentNullException ex) when (ex.ParamName == "type")
{
    // matcherType was null: fix type resolution before retrying
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of devlooped/moq@89a5be629c (2026-09-16). Data as JSON: /api/errors/dd575fdab4e1f03f. Report an issue: GitHub.

Appendix: source

Thrown at src/Moq/TypeMatcherAttribute.cs:42

        ///     Use this constructor overload if the type on which this attribute is placed implements <see cref="ITypeMatcher"/> itself.
        ///   </para>
        /// </summary>
        public TypeMatcherAttribute()
        {
            this.type = null;
        }

        /// <summary>
        ///   Initializes a new instance of the <see cref="TypeMatcherAttribute"/> class.
        ///   <para>
        ///     Use this constructor overload if the type on which this attribute is placed does not implement <see cref="ITypeMatcher"/>.
        ///     The specified type will instead provide the implementation of <see cref="ITypeMatcher"/>.
        ///   </para>
        /// </summary>
        /// <param name="type">The <see cref="Type"/> of a type that implements <see cref="ITypeMatcher"/>.</param>
        public TypeMatcherAttribute(Type type)
        {
            this.type = type ?? throw new ArgumentNullException(nameof(type));
        }

        internal Type? Type => this.type;
    }
}

View on GitHub (pinned to 89a5be629c)