DapperLib/Dapper · error · ArgumentNullException

type

Error message

type

What it means

CustomPropertyTypeMap's constructor rejects a null type argument with ArgumentNullException(nameof(type)). The map needs the target entity Type to resolve properties at construction time, so a null type is unrecoverable. The same guard applies to the propertySelector delegate.

Source

Thrown at Dapper/CustomPropertyTypeMap.cs:21

namespace Dapper
{
    /// <summary>
    /// Implements custom property mapping by user provided criteria (usually presence of some custom attribute with column to member mapping)
    /// </summary>
    public sealed class CustomPropertyTypeMap : SqlMapper.ITypeMap
    {
        private readonly Type _type;
        private readonly Func<Type, string, PropertyInfo> _propertySelector;

        /// <summary>
        /// Creates custom property mapping
        /// </summary>
        /// <param name="type">Target entity type</param>
        /// <param name="propertySelector">Property selector based on target type and DataReader column name</param>
        public CustomPropertyTypeMap(Type type, Func<Type, string, PropertyInfo> propertySelector)
        {
            _type = type ?? throw new ArgumentNullException(nameof(type));
            _propertySelector = propertySelector ?? throw new ArgumentNullException(nameof(propertySelector));
        }

        /// <summary>
        /// Always returns default constructor
        /// </summary>
        /// <param name="names">DataReader column names</param>
        /// <param name="types">DataReader column types</param>
        /// <returns>Default constructor</returns>
        public ConstructorInfo? FindConstructor(string[] names, Type[] types) =>
            _type.GetConstructor(Array.Empty<Type>())!;

        /// <summary>
        /// Always returns null
        /// </summary>
        /// <returns></returns>
        public ConstructorInfo? FindExplicitConstructor() => null;

View on GitHub (pinned to 72a54c475f)

Solutions

  1. Resolve the Type before construction and throw a clearer error if null (e.g. guard Type.GetType results).
  2. Verify the assembly-qualified type name string is correct and the assembly is loaded/referenced.

Example fix

// before
var map = new CustomPropertyTypeMap(Type.GetType(configType), (t, col) => ...);

// after
var type = Type.GetType(configType) ?? throw new InvalidOperationException($"Unknown type: {configType}");
var map = new CustomPropertyTypeMap(type, (t, col) => ...);
Defensive patterns

Strategy: validation

Validate before calling

var type = Type.GetType(modelName) ?? throw new InvalidOperationException($"Unknown type: {modelName}");
var map = new CustomPropertyTypeMap(type, (t, col) => t.GetProperty(col));

Type guard

static Type RequireType(Type? t, string name) => t ?? throw new ArgumentNullException(name);

Prevention

When it happens

Trigger: Constructing new CustomPropertyTypeMap(null, selector) or passing a type resolved dynamically that came back null (e.g. Type.GetType("Missing.Namespace.Foo") returned null). Registering a custom map via SqlMapper.SetTypeMap with a null type.

Common situations: Type.GetType with a misspelled/assembly-qualified name returning null; reflection-driven code that loads entity types from config where a name was wrong; generics where typeof() was replaced by a variable that lost its value.

Related errors


AI-assisted analysis of DapperLib/Dapper@72a54c475f (2026-08-13). Data as JSON: /api/errors/b616bb46415bf840. Report an issue: GitHub.