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 FieldView on GitHub (pinned to 6ece18c5c2)
Solutions
- Make the generic parameter and the Type argument identical, e.g. RuleForType<int>(typeof(int), ...).
- Remove the explicit Type argument and rely on inference only if your helper passes typeof(TType) consistently.
- 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
- Never pass an independent typeof(...) that could diverge from the generic parameter.
- Wrap RuleForType in a helper that closes both from one source.
- Add a unit test asserting typeof(TType)==type for each call site.
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
- Cannot create a rule set within a rule set.
- alphabet must contain at least 4 unique characters.
- A premium license is required to use this API. Please double
- Your expression '{expressionString}' cant be used. Nested ac
- Expression was not of the form 'x => x.Property or x => x.Fi
AI-assisted analysis of bchavez/Bogus@6ece18c5c2 (2026-08-13).
Data as JSON: /api/errors/3587e8cbd7f63b4b.
Report an issue: GitHub.