{"record":{"id":"a270e933e0ba01f2","repo":"bchavez/Bogus","slug":"when-calling-enum-t-with-no-parameters-t-must-b","errorCode":null,"errorMessage":"When calling Enum<T>() with no parameters T must be an enum.","messagePattern":"When calling Enum<T>\\(\\) with no parameters T must be an enum\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Source/Bogus/Randomizer.cs","lineNumber":659,"sourceCode":"      if( min != null && min > str.Length )\n      {\n         var missingChars = min - str.Length;\n         var fillerChars = this.Replace(\"\".PadRight(missingChars.Value, '?'));\n         return str + fillerChars;\n      }\n      return str;\n   }\n\n   /// <summary>\n   /// Picks a random enum value in T:Enum.\n   /// </summary>\n   /// <typeparam name=\"T\">Must be an Enum</typeparam>\n   /// <param name=\"exclude\">Exclude enum values from being returned</param>\n   public T Enum<T>(params T[] exclude) where T : struct, Enum\n   {\n      var e = typeof(T);\n      if( !e.IsEnum() )\n         throw new ArgumentException(\"When calling Enum<T>() with no parameters T must be an enum.\");\n\n      var selection = System.Enum.GetNames(e);\n\n      if( exclude.Any() )\n      {\n         var excluded = exclude.Select(ex => System.Enum.GetName(e, ex));\n         selection = selection.Except(excluded).ToArray();\n      }\n\n      if( !selection.Any() )\n      {\n         throw new ArgumentException(\"There are no values after exclusion to choose from.\");\n      }\n\n      var val = this.ArrayElement(selection);\n\n      System.Enum.TryParse(val, out T picked);\n      return picked;","sourceCodeStart":641,"sourceCodeEnd":677,"githubUrl":"https://github.com/bchavez/Bogus/blob/6ece18c5c22648ab4606e45a0aaf2f07fc5259e4/Source/Bogus/Randomizer.cs#L641-L677","documentation":"Thrown by Randomizer.Enum<T> when typeof(T).IsEnum() is false. Despite the generic constraint 'where T : struct, Enum' (which prevents most misuse), the runtime check guards against edge cases where T passes the constraint but IsEnum reports false. The method needs an enum to enumerate values.","triggerScenarios":"Calling f.Random.Enum<int>() through reflection or dynamic generic construction that bypasses compile-time constraints; historically called before the Enum constraint existed.","commonSituations":"Older code targeting a Bogus version without the generic constraint; reflection-based generic instantiation.","solutions":["Pass a real enum type as T, e.g. f.Random.Enum<MyEnum>().","Upgrade Bogus so the compile-time 'where T : Enum' constraint catches misuse.","If using reflection, verify typeof(T).IsEnum before invoking."],"exampleFix":"// before\nvar v = f.Random.Enum<int>();\n// after\nvar v = f.Random.Enum<MyEnum>();","handlingStrategy":"type-guard","validationCode":"static T SafeEnum<T>(Bogus.Randomizer r) where T : struct, Enum {\n    return r.Enum<T>();\n}","typeGuard":"static bool IsEnumType<T>() => typeof(T).IsEnum;","tryCatchPattern":"try { return r.Enum<T>(); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"must be an enum\")) {\n    // switch to a real enum type\n}","preventionTips":["Always pass an enum type as T.","Upgrade Bogus to a version with the 'where T : struct, Enum' constraint.","If using reflection, assert typeof(T).IsEnum first."],"tags":["bogus","randomizer","enum","type-validation"],"backgroundTag":null,"analyzedSha":"6ece18c5c22648ab4606e45a0aaf2f07fc5259e4","analyzedAt":"2026-08-13T21:08:15.463Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}