{"record":{"id":"3587e8cbd7f63b4b","repo":"bchavez/Bogus","slug":"nameof-ttype-must-be-the-same-type-as-parameter","errorCode":null,"errorMessage":"{nameof(TType)} must be the same type as parameter named '{nameof(type)}'","messagePattern":"(.+?) must be the same type as parameter named '(.+?)'","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Source/Bogus/Faker[T].cs","lineNumber":311,"sourceCode":"            RuleSet = currentRuleSet,\n            PropertyName = guid,\n            ProhibitInStrictMode = true\n         };\n      this.Actions.Add(currentRuleSet, guid, rule);\n      return this;\n   }\n\n   /// <summary>\n   /// Creates one rule for all types of <typeparamref name=\"TType\"/> on type <typeparamref name=\"T\"/>.\n   /// In other words, if you have <typeparamref name=\"T\"/> with many fields or properties of\n   /// type <seealso cref=\"Int32\"/> this method allows you to specify a rule for all fields or\n   /// properties of type <seealso cref=\"Int32\"/>.\n   /// </summary>\n   public virtual Faker<T> RuleForType<TType>(Type type, Func<Faker, TType> setterForType)\n   {\n      if( typeof(TType) != type )\n      {\n         throw new ArgumentException($\"{nameof(TType)} must be the same type as parameter named '{nameof(type)}'\");\n      }\n\n      foreach( var kvp in this.TypeProperties )\n      {\n         var propOrFieldType = GetFieldOrPropertyType(kvp.Value);\n         var propOrFieldName = kvp.Key;\n\n         if( propOrFieldType == type )\n         {\n            RuleFor(propOrFieldName, setterForType);\n         }\n      }\n\n      return this;\n   }\n\n   /// <summary>\n   /// Utility method to get the Type of a Property or Field","sourceCodeStart":293,"sourceCodeEnd":329,"githubUrl":"https://github.com/bchavez/Bogus/blob/6ece18c5c22648ab4606e45a0aaf2f07fc5259e4/Source/Bogus/Faker[T].cs#L293-L329","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nfaker.RuleForType<int>(typeof(string), f => 0);\n// after\nfaker.RuleForType<int>(typeof(int), f => f.Random.Number(0, 100));","handlingStrategy":"type-guard","validationCode":"static Faker<T> RuleForTypeSafe<TType>(Faker<T> f, Func<Faker,TType> setter) where TType : notnull {\n    // TType and typeof(TType) are guaranteed equal by construction\n    return f.RuleForType<TType>(typeof(TType), setter);\n}","typeGuard":"// no runtime guard needed if the Type argument is always typeof(TType);\n// otherwise:\nif (typeof(TType) != type) throw new InvalidOperationException(\"type mismatch\");","tryCatchPattern":"try { faker.RuleForType<T>(typeof(T), setter); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"must be the same type\")) {\n    // align generic and Type arguments, then retry with corrected call\n}","preventionTips":["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."],"tags":["bogus","rulefortype","type-mismatch","generic"],"backgroundTag":null,"analyzedSha":"6ece18c5c22648ab4606e45a0aaf2f07fc5259e4","analyzedAt":"2026-08-13T21:08:15.463Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}