DapperLib/Dapper · error · InvalidOperationException

Member '{name}' is an {nameof(ICustomQueryParameter)} and ca

Error message

Member '{name}' is an {nameof(ICustomQueryParameter)} and cannot be null

What it means

Thrown (via ThrowNullCustomQueryParameter, SqlMapper.cs:3945) from generated parameter-setting IL when a property/field that implements ICustomQueryParameter (e.g. Dapper's DbString) is null on the parameter object. Dapper calls `ICustomQueryParameter.AddParameter` on the member; a null instance cannot have AddParameter called, so the emitted null-check calls ThrowNullCustomQueryParameter with the member name.

Source

Thrown at Dapper/SqlMapper.cs:3945

        private static MethodInfo? ResolveOperator(MethodInfo[] methods, Type from, Type to, string name)
        {
            for (int i = 0; i < methods.Length; i++)
            {
                if (methods[i].Name != name || methods[i].ReturnType != to) continue;
                var args = methods[i].GetParameters();
                if (args.Length != 1 || args[0].ParameterType != from) continue;
                return methods[i];
            }
            return null;
        }

        /// <summary>
        /// For internal use only
        /// </summary>
        [Obsolete(ObsoleteInternalUsageOnly, false)]
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public static void ThrowNullCustomQueryParameter(string name)
            => throw new InvalidOperationException($"Member '{name}' is an {nameof(ICustomQueryParameter)} and cannot be null");

        /// <summary>
        /// Throws a data exception, only used internally
        /// </summary>
        /// <param name="ex">The exception to throw.</param>
        /// <param name="index">The index the exception occurred at.</param>
        /// <param name="reader">The reader the exception occurred in.</param>
        /// <param name="value">The value that caused the exception.</param>
        [Obsolete(ObsoleteInternalUsageOnly, false)]
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public static void ThrowDataException(Exception ex, int index, IDataReader reader, object? value)
        {
            Exception toThrow;
            try
            {
                string name = "(n/a)", formattedValue = "(n/a)";
                if (reader is not null && index >= 0 && index < reader.FieldCount)
                {

View on GitHub (pinned to 72a54c475f)

Solutions

  1. Initialize the DbString/ICustomQueryParameter member to a non-null value (e.g. `new DbString { Value = name, IsAnsi = true, Length = 100 }`).
  2. Make the member nullable and omit it from the parameter set when not needed (use a conditional parameter object or DynamicParameters).
  3. Use a plain `string` property instead of DbString when you do not need ANSI/length control.
  4. Build the parameter object so that DbString members are always populated before the query.

Example fix

// before
new { Name = (DbString)null }
// after
new { Name = new DbString { Value = name, IsAnsi = true, Length = 50 } }
Defensive patterns

Strategy: validation

Validate before calling

// Ensure DbString/ICustomQueryParameter members are non-null before the query.
if (p.Name is null) p.Name = new DbString { Value = name ?? (object)DBNull.Value, IsAnsi = true, Length = 50 };

Type guard

static bool AllCustomParamsSet(object p) => p.GetType().GetProperties().Where(pr => typeof(ICustomQueryParameter).IsAssignableFrom(pr.PropertyType)).All(pr => pr.GetValue(p) is not null);

Prevention

When it happens

Trigger: A parameter object with a property of type DbString (or any ICustomQueryParameter) left null: `new Foo { Name = null }` where `public DbString Name`. The generated IL dup-checks the value and, if null, throws naming the member.

Common situations: Conditionally building a filter where a DbString property is only sometimes set and left null otherwise; reusing a DTO whose DbString member was never initialized; assuming DbString defaults like a normal string.

Related errors


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