bchavez/Bogus · error · ArgumentException

{nameof(TType)} must be the same type as parameter named '{n

Error message

{nameof(TType)} must be the same type as parameter named '{nameof(type)}'

What it means

Thrown by Faker<T>.RuleForType<TType> when the generic type parameter TType does not match the System.Type passed as the 'type' argument. The method applies a single setter to every field/property of a given type on T, so both type specifications must agree. The mismatch makes the intent ambiguous and is rejected.

Source

Thrown at Source/Bogus/Faker[T].cs:311

            RuleSet = currentRuleSet,
            PropertyName = guid,
            ProhibitInStrictMode = true
         };
      this.Actions.Add(currentRuleSet, guid, rule);
      return this;
   }

   /// <summary>
   /// Creates one rule for all types of <typeparamref name="TType"/> on type <typeparamref name="T"/>.
   /// In other words, if you have <typeparamref name="T"/> with many fields or properties of
   /// type <seealso cref="Int32"/> this method allows you to specify a rule for all fields or
   /// properties of type <seealso cref="Int32"/>.
   /// </summary>
   public virtual Faker<T> RuleForType<TType>(Type type, Func<Faker, TType> setterForType)
   {
      if( typeof(TType) != type )
      {
         throw new ArgumentException($"{nameof(TType)} must be the same type as parameter named '{nameof(type)}'");
      }

      foreach( var kvp in this.TypeProperties )
      {
         var propOrFieldType = GetFieldOrPropertyType(kvp.Value);
         var propOrFieldName = kvp.Key;

         if( propOrFieldType == type )
         {
            RuleFor(propOrFieldName, setterForType);
         }
      }

      return this;
   }

   /// <summary>
   /// Utility method to get the Type of a Property or Field

View on GitHub (pinned to 6ece18c5c2)

Solutions

  1. Make the generic parameter and the Type argument identical, e.g. RuleForType<int>(typeof(int), ...).
  2. Remove the explicit Type argument and rely on inference only if your helper passes typeof(TType) consistently.
  3. If building dynamically, resolve the Type from the same source you use to close the generic.

Example fix

// before
faker.RuleForType<int>(typeof(string), f => 0);
// after
faker.RuleForType<int>(typeof(int), f => f.Random.Number(0, 100));
Defensive patterns

Strategy: type-guard

Validate before calling

static Faker<T> RuleForTypeSafe<TType>(Faker<T> f, Func<Faker,TType> setter) where TType : notnull {
    // TType and typeof(TType) are guaranteed equal by construction
    return f.RuleForType<TType>(typeof(TType), setter);
}

Type guard

// no runtime guard needed if the Type argument is always typeof(TType);
// otherwise:
if (typeof(TType) != type) throw new InvalidOperationException("type mismatch");

Try / catch

try { faker.RuleForType<T>(typeof(T), setter); }
catch (ArgumentException ex) when (ex.Message.Contains("must be the same type")) {
    // align generic and Type arguments, then retry with corrected call
}

Prevention

When it happens

Trigger: Calling faker.RuleForType<int>(typeof(string), (f) => 5) where the generic parameter (int) differs from the runtime Type argument (typeof(string)). Any call where typeof(TType) != type parameter.

Common situations: Passing typeof(SomeType) hardcoded while inferring TType from a variable, or copy-pasting a RuleForType call and forgetting to update one of the two type slots.

Related errors


AI-assisted analysis of bchavez/Bogus@6ece18c5c2 (2026-08-13). Data as JSON: /api/errors/3587e8cbd7f63b4b. Report an issue: GitHub.