DapperLib/Dapper · error · ArgumentNullException

type

Error message

type

What it means

DefaultTypeMap's constructor rejects a null type with ArgumentNullException(nameof(type)). The map immediately calls GetSettableFields/GetSettableProps on the type, which would NullReferenceException without this guard. This is the standard type-map entry point used internally by Dapper for materialization.

Source

Thrown at Dapper/DefaultTypeMap.cs:23

namespace Dapper
{
    /// <summary>
    /// Represents default type mapping strategy used by Dapper
    /// </summary>
    public sealed class DefaultTypeMap : SqlMapper.ITypeMap
    {
        private readonly FieldInfo[] _fields;
        private readonly Type _type;

        /// <summary>
        /// Creates default type map
        /// </summary>
        /// <param name="type">Entity type</param>
        public DefaultTypeMap(Type type)
        {
            if (type is null)
                throw new ArgumentNullException(nameof(type));

            _fields = GetSettableFields(type);
            Properties = GetSettableProps(type);
            _type = type;
        }

        internal static MethodInfo GetPropertySetterOrThrow(PropertyInfo propertyInfo, Type type)
        {
            return GetPropertySetter(propertyInfo, type) ?? Throw(propertyInfo);

            static MethodInfo Throw(PropertyInfo propertyInfo) => throw new InvalidOperationException("Property setting not found for: " + propertyInfo?.Name);
        }
        internal static MethodInfo? GetPropertySetter(PropertyInfo propertyInfo, Type type)
        {
            if (propertyInfo.DeclaringType == type) return propertyInfo.GetSetMethod(true);

            return propertyInfo.DeclaringType!.GetProperty(
                   propertyInfo.Name,

View on GitHub (pinned to 72a54c475f)

Solutions

  1. Validate the Type is non-null before constructing the map; guard Type.GetType results explicitly.
  2. Correct the assembly-qualified type name string and ensure the containing assembly is loaded.

Example fix

// before
var map = new DefaultTypeMap(Type.GetType(modelName));

// after
var type = Type.GetType(modelName) ?? throw new InvalidOperationException($"Unknown type: {modelName}");
var map = new DefaultTypeMap(type);
Defensive patterns

Strategy: validation

Validate before calling

var type = Type.GetType(modelName) ?? throw new InvalidOperationException($"Unknown type: {modelName}");
var map = new DefaultTypeMap(type);

Type guard

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

Prevention

When it happens

Trigger: Constructing new DefaultTypeMap(null); Dapper internals attempting to build a type map for a type that resolved to null (e.g. Type.GetType on a bad name); calling SqlMapper.SetTypeMap(type, map) where type is null.

Common situations: Reflection-based type loading where Type.GetType returned null because the assembly-qualified name was wrong or the assembly was not referenced; dynamically building maps from config with a missing type entry.

Related errors


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