Skip to content

Commit 4a976b7

Browse files
authored
Add some enum generic constraints (#31515)
1 parent 221ea0b commit 4a976b7

File tree

10 files changed

+7
-45
lines changed

10 files changed

+7
-45
lines changed

src/EFCore/Storage/Json/JsonSignedEnumReaderWriter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Microsoft.EntityFrameworkCore.Storage.Json;
99
/// Reads and writes JSON for <see langword="enum" /> values backed by a signed integer.
1010
/// </summary>
1111
public sealed class JsonSignedEnumReaderWriter<TEnum> : JsonValueReaderWriter<TEnum>
12+
where TEnum : struct, Enum
1213
{
1314
/// <summary>
1415
/// The singleton instance of this stateless reader/writer.

src/EFCore/Storage/Json/JsonUnsignedEnumReaderWriter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Microsoft.EntityFrameworkCore.Storage.Json;
99
/// Reads and writes JSON for <see langword="enum" /> values backed by an unsigned integer.
1010
/// </summary>
1111
public sealed class JsonUnsignedEnumReaderWriter<TEnum> : JsonValueReaderWriter<TEnum>
12+
where TEnum : struct, Enum
1213
{
1314
/// <summary>
1415
/// The singleton instance of this stateless reader/writer.

src/EFCore/Storage/Json/JsonWarningEnumReaderWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Microsoft.EntityFrameworkCore.Storage.Json;
1010
/// happens, a warning is generated.
1111
/// </summary>
1212
public sealed class JsonWarningEnumReaderWriter<TEnum> : JsonValueReaderWriter<TEnum>
13-
where TEnum : struct
13+
where TEnum : struct, Enum
1414
{
1515
/// <summary>
1616
/// The singleton instance of this stateless reader/writer.

src/EFCore/Storage/ValueConversion/EnumToNumberConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion;
1010
/// See <see href="https://aka.ms/efcore-docs-value-converters">EF Core value converters</see> for more information and examples.
1111
/// </remarks>
1212
public class EnumToNumberConverter<TEnum, TNumber> : ValueConverter<TEnum, TNumber>
13-
where TEnum : struct
13+
where TEnum : struct, Enum
1414
where TNumber : struct
1515
{
1616
// ReSharper disable once StaticMemberInGenericType

src/EFCore/Storage/ValueConversion/EnumToStringConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion;
1212
/// See <see href="https://aka.ms/efcore-docs-value-converters">EF Core value converters</see> for more information and examples.
1313
/// </remarks>
1414
public class EnumToStringConverter<TEnum> : StringEnumConverter<TEnum, string, TEnum>
15-
where TEnum : struct
15+
where TEnum : struct, Enum
1616
{
1717
/// <summary>
1818
/// Creates a new instance of this converter. This converter does not preserve order.

src/EFCore/Storage/ValueConversion/Internal/StringEnumConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
1010
/// doing so can result in application failures when updating to a new Entity Framework Core release.
1111
/// </summary>
1212
public class StringEnumConverter<TModel, TProvider, TEnum> : ValueConverter<TModel, TProvider>
13-
where TEnum : struct
13+
where TEnum : struct, Enum
1414
{
1515
/// <summary>
1616
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to

src/EFCore/Storage/ValueConversion/StringToEnumConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion;
1212
/// See <see href="https://aka.ms/efcore-docs-value-converters">EF Core value converters</see> for more information and examples.
1313
/// </remarks>
1414
public class StringToEnumConverter<TEnum> : StringEnumConverter<string, TEnum, TEnum>
15-
where TEnum : struct
15+
where TEnum : struct, Enum
1616
{
1717
/// <summary>
1818
/// Creates a new instance of this converter. This converter does not preserve order.

test/EFCore.Tests/Storage/EnumToNumberConverterTest.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -265,26 +265,6 @@ public void Can_convert_ulongs_to_enums()
265265
Assert.Equal(default, converter(0));
266266
}
267267

268-
[ConditionalFact]
269-
public void Enum_to_integer_converter_throws_for_bad_types()
270-
{
271-
Assert.Equal(
272-
CoreStrings.ConverterBadType(
273-
typeof(EnumToNumberConverter<Guid, int>).ShortDisplayName(),
274-
"Guid",
275-
"enum types"),
276-
Assert.Throws<InvalidOperationException>(
277-
() => new EnumToNumberConverter<Guid, int>()).Message);
278-
279-
Assert.Equal(
280-
CoreStrings.ConverterBadType(
281-
typeof(EnumToNumberConverter<Beatles, Guid>).ShortDisplayName(),
282-
"Guid",
283-
"'int', 'long', 'short', 'byte', 'uint', 'ulong', 'ushort', 'sbyte', 'double', 'float', 'decimal'"),
284-
Assert.Throws<InvalidOperationException>(
285-
() => new EnumToNumberConverter<Beatles, Guid>()).Message);
286-
}
287-
288268
private enum Beatles
289269
{
290270
John = 7,

test/EFCore.Tests/Storage/EnumToStringConverterTest.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,6 @@ public void Can_convert_strings_to_enums_object()
7777
Assert.Null(converter(null));
7878
}
7979

80-
[ConditionalFact]
81-
public void Enum_to_string_converter_throws_for_bad_types()
82-
=> Assert.Equal(
83-
CoreStrings.ConverterBadType(
84-
typeof(StringEnumConverter<Guid, string, Guid>).ShortDisplayName(),
85-
"Guid",
86-
"enum types"),
87-
Assert.Throws<InvalidOperationException>(
88-
() => new EnumToStringConverter<Guid>()).Message);
89-
9080
private enum Beatles
9181
{
9282
John = 7,

test/EFCore.Tests/Storage/StringToEnumConverterTest.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,6 @@ public void Can_convert_enums_to_strings_object()
7878
Assert.Null(converter(null));
7979
}
8080

81-
[ConditionalFact]
82-
public void String_to_enum_converter_throws_for_bad_types()
83-
=> Assert.Equal(
84-
CoreStrings.ConverterBadType(
85-
typeof(StringEnumConverter<string, Guid, Guid>).ShortDisplayName(),
86-
"Guid",
87-
"enum types"),
88-
Assert.Throws<InvalidOperationException>(
89-
() => new StringToEnumConverter<Guid>()).Message);
90-
9181
private enum Beatles
9282
{
9383
John = 7,

0 commit comments

Comments
 (0)