Edit

Share via


EnumConverter validates registered types to be enum

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.

Previous behavior

Previously, the type to be registered was not validated to be an enum type.

New behavior

Starting in .NET 9, EnumConverter throws an ArgumentException if the type to be converted is not an enum type. Any derived classes of EnumConverter should also respect this requirement.

Version introduced

.NET 9 Preview 7

Type of breaking change

This change is a behavioral change.

Reason for change

It is logical to enforce the requirement that EnumConverter be used to convert to and from enum types only. It was likely an oversight that this requirement wasn't added earlier.

However, the primary driving factor for this change was for trimming purposes. Trimming doesn't trim enum types, but using EnumConverter for enum types required 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.

There is no easy workaround if an EnumConverter is used to convert to and from a non-enum type.

Affected APIs