Skip to content

Commit

Permalink
Use C# 7.3 Enum constraints.
Browse files Browse the repository at this point in the history
  • Loading branch information
bchavez committed May 21, 2018
1 parent f1af5ab commit 127e10c
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## v22.1.1
* PR 144: Argument support for mustache handlebars. Example: `{{name.firstname(Male)}}`
* Using **C# 7.3** generic `Enum` constraints for methods that only accept enums. Example: `f.PickRandom<Enum>()`.

## v22.0.9
* Issue 143: Fixed rare case when `f.IndexGlobal` could be zero twice at start of generation.
Expand Down
2 changes: 1 addition & 1 deletion Source/Bogus/Bogus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Version>0.0.0-localbuild</Version>
<Authors>Brian Chavez</Authors>
<TargetFrameworks>net40;netstandard1.3;netstandard2.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<LangVersion>7.3</LangVersion>
<CodeAnalysisRuleSet>Bogus.ruleset</CodeAnalysisRuleSet>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyOriginatorKeyFile>
Expand Down
4 changes: 2 additions & 2 deletions Source/Bogus/Faker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ public IEnumerable<T> MakeLazy<T>(int count, Func<int, T> action)
/// Picks a random Enum of T. Works only with Enums.
/// </summary>
/// <typeparam name="T">Must be an Enum</typeparam>
public T PickRandom<T>() where T : struct
public T PickRandom<T>() where T : struct, Enum
{
return this.Random.Enum<T>();
}
Expand All @@ -301,7 +301,7 @@ public IEnumerable<T> MakeLazy<T>(int count, Func<int, T> action)
/// Picks a random Enum of T, excluding those passed as parameters.
/// </summary>
/// <param name="exclude">The items in the Enum of T to exclude from selection.</param>
public T PickRandomWithout<T>(params T[] exclude) where T : struct
public T PickRandomWithout<T>(params T[] exclude) where T : struct, Enum
{
return this.Random.Enum(exclude);
}
Expand Down
5 changes: 2 additions & 3 deletions Source/Bogus/Randomizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ public string ClampString(string str, int? min = null, int? max = null)
/// </summary>
/// <typeparam name="T">Must be an Enum</typeparam>
/// <param name="exclude">Exclude enum values from being returned</param>
public T Enum<T>(params T[] exclude) where T : struct
public T Enum<T>(params T[] exclude) where T : struct, Enum
{
var e = typeof(T);
if( !e.IsEnum() )
Expand All @@ -558,8 +558,7 @@ public string ClampString(string str, int? min = null, int? max = null)

var val = this.ArrayElement(selection);

T picked;
System.Enum.TryParse(val, out picked);
System.Enum.TryParse(val, out T picked);
return picked;
}

Expand Down

0 comments on commit 127e10c

Please sign in to comment.