Skip to content

Commit f21aff3

Browse files
authored
EnumConverter breaking change (#42847)
1 parent 542b430 commit f21aff3

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

docs/core/compatibility/9.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ If you're migrating an app to .NET 9, the breaking changes listed here might aff
3939
| [BinaryReader.GetString() returns "\uFFFD" on malformed sequences](core-libraries/9.0/binaryreader.md) | Behavioral change | Preview 7 |
4040
| [Creating type of array of System.Void not allowed](core-libraries/9.0/type-instance.md) | Behavioral change | Preview 1 |
4141
| [Default `Equals()` and `GetHashCode()` throw for types marked with `InlineArrayAttribute`](core-libraries/9.0/inlinearrayattribute.md) | Behavioral change | Preview 6 |
42+
| [EnumConverter validates registered types to be enum](core-libraries/9.0/enumconverter.md) | Behavioral change | Preview 7 |
4243
| [FromKeyedServicesAttribute no longer injects non-keyed parameter](core-libraries/9.0/non-keyed-params.md) | Behavioral change | RC 1 |
4344
| [IncrementingPollingCounter initial callback is asynchronous](core-libraries/9.0/async-callback.md) | Behavioral change | RC 1 |
4445
| [Inline array struct size limit is enforced](core-libraries/9.0/inlinearray-size.md) | Behavioral change | Preview 1 |
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
title: "Breaking change: EnumConverter validates registered types to be enum"
3+
description: Learn about the .NET 9 breaking change where EnumConverter now validates that the type to be registered is an enum type.
4+
ms.date: 10/04/2024
5+
---
6+
# EnumConverter validates registered types to be enum
7+
8+
<xref:System.ComponentModel.EnumConverter> is a type converter that converts to and from an `enum` type. EnumConverter now validates that the type to be registered is of an `enum` type.
9+
10+
## Previous behavior
11+
12+
Previously, the type to be registered was not validated to be an `enum` type.
13+
14+
## New behavior
15+
16+
Starting in .NET 9, <xref:System.ComponentModel.EnumConverter> throws an <xref:System.ArgumentException> if the type to be converted is not an `enum` type. Any derived classes of <xref:System.ComponentModel.EnumConverter> should also respect this requirement.
17+
18+
## Version introduced
19+
20+
.NET 9 Preview 7
21+
22+
## Type of breaking change
23+
24+
This change is a [behavioral change](../../categories.md#behavioral-change).
25+
26+
## Reason for change
27+
28+
It is logical to enforce the requirement that <xref:System.ComponentModel.EnumConverter> be used to convert to and from `enum` types only. It was likely an oversight that this requirement wasn't added earlier.
29+
30+
However, the primary driving factor for this change was for trimming purposes. [Trimming](../../../deploying/trimming/prepare-libraries-for-trimming.md) doesn't trim `enum` types, but using <xref:System.ComponentModel.EnumConverter> for `enum` types required <xref:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute> annotation for an `enum` type. This means that using `EnumConverter` generates unnecessary trim warnings. A recent change removed the annotation requirement. Part of the reason for that change was to enforce that `EnumConverter` only be used with `enums`.
31+
32+
## Recommended action
33+
34+
There is no easy workaround if an <xref:System.ComponentModel.EnumConverter> is used to convert to and from a non-`enum` type.
35+
36+
## Affected APIs
37+
38+
- <xref:System.ComponentModel.EnumConverter.%23ctor(System.Type)> constructor

docs/core/compatibility/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ items:
3030
href: core-libraries/9.0/biginteger-limit.md
3131
- name: Creating type of array of System.Void not allowed
3232
href: core-libraries/9.0/type-instance.md
33+
- name: EnumConverter validates registered types to be enum
34+
href: core-libraries/9.0/enumconverter.md
3335
- name: "`Equals`/`GetHashCode` throw for `InlineArrayAttribute` types"
3436
href: core-libraries/9.0/inlinearrayattribute.md
3537
- name: FromKeyedServicesAttribute no longer injects non-keyed parameter
@@ -1260,6 +1262,8 @@ items:
12601262
href: core-libraries/9.0/binaryreader.md
12611263
- name: Creating type of array of System.Void not allowed
12621264
href: core-libraries/9.0/type-instance.md
1265+
- name: EnumConverter validates registered types to be enum
1266+
href: core-libraries/9.0/enumconverter.md
12631267
- name: "`Equals`/`GetHashCode` throw for `InlineArrayAttribute` types"
12641268
href: core-libraries/9.0/inlinearrayattribute.md
12651269
- name: FromKeyedServicesAttribute no longer injects non-keyed parameter

0 commit comments

Comments
 (0)