When creating this library, I deliberately tried to make the API and behavior as close to System.Enum's as possible in order for it to be a drop in replacement as well as allowing Enums.NET to be the basis for improvements to System.Enum in corefx as has been proposed with corefx#15453.
There are however a few behavioral differences that either snuck in or were made deliberately. If corefx#15453 gets implemented I will like to update Enums.NET to a new major version which fixes most of these behavioral differences.
| API |
Enums.NET Behavior |
System.Enum Behavior |
Proposed Solution |
GetNames |
Names are returned in increasing order by their respective values |
Names are returned in increasing significance bit order by their respective values |
Add an EnumMemberSelection.ValueOrder flag enum member |
GetValues |
Values are returned in increasing value order |
Values are returned in increasing significance bit order |
Add an EnumMemberSelection.ValueOrder flag enum member |
GetMembers |
Members are returned in increasing order by their respective values |
Members will be returned in increasing significance bit order by their respective values |
Add an EnumMemberSelection.ValueOrder flag enum member |
ToObject |
Throws OverflowException when value is outside the underlying type's value range |
Allows overflowing values |
Rework EnumValidation enum and add an EnumValidation.NoOverflow flag enum member. EnumValidation.None, EnumValidation.IsDefined, and EnumValidation.IsValidFlagCombination will have new meanings such that they no longer validate there were no overflows. |
AsString and Format with flags |
For flags, if the value is not defined then each of it's individual flags are output |
For flags, the defined combinations within the value with the largest values are output until their are no more flags left to output |
Hard break in Enums.NET. To get the same behavior as before you'll need to replace it with something like this. value.IsValidFlagCombination() ? string.Join(", ", value.GetFlagMembers().Select(flag => flag.Name) : value.AsString() |
Additionally, more research must be done to determine the behavioral differences for the following methods with their accepted value types and the exceptions they throw.
Format(Type enumType, object value, string format)
GetName(Type enumType, object value)
IsDefined(Type enumType, object value)
Other potential breaking changes if corefx#15453 gets implemented
- Change
EnumsNET.PrimaryEnumMemberAttribute to System.ComponentModel.PrimaryAttribute.
- Switch to using
System.ComponentModel.AttributeCollection and then add appropriate extension methods to support the existing methods on EnumsNET.AttributeCollection in older versions of System.ComponentModel.AttributeCollection. This will mean in older framework targets the attribute collection will not explicitly implement IList<Attribute> or IReadOnlyList<Attribute> as is currently the case but will support being casted to those interfaces as the returned attribute collection will derive from System.ComponentModel.AttributeCollection for older framework targets.
- Change
GetAllFlags to AllFlags as in the proposal.
IsValidFlagCombination will be promoted to an extension method in older framework targets.
- The existing extension methods that are covered in the proposal will be demoted in newer framework targets so users will be encouraged to use the new built-in extension methods and not cause any overload resolution conflicts.
- Remove
EnumMember in newer framework targets that already contain the built-in EnumMember and use that instead.
When creating this library, I deliberately tried to make the API and behavior as close to
System.Enum's as possible in order for it to be a drop in replacement as well as allowingEnums.NETto be the basis for improvements toSystem.Enumin corefx as has been proposed with corefx#15453.There are however a few behavioral differences that either snuck in or were made deliberately. If corefx#15453 gets implemented I will like to update
Enums.NETto a new major version which fixes most of these behavioral differences.Enums.NETBehaviorSystem.EnumBehaviorGetNamesEnumMemberSelection.ValueOrderflag enum memberGetValuesEnumMemberSelection.ValueOrderflag enum memberGetMembersEnumMemberSelection.ValueOrderflag enum memberToObjectOverflowExceptionwhen value is outside the underlying type's value rangeEnumValidationenum and add anEnumValidation.NoOverflowflag enum member.EnumValidation.None,EnumValidation.IsDefined, andEnumValidation.IsValidFlagCombinationwill have new meanings such that they no longer validate there were no overflows.AsStringandFormatwith flagsEnums.NET. To get the same behavior as before you'll need to replace it with something like this.value.IsValidFlagCombination() ? string.Join(", ", value.GetFlagMembers().Select(flag => flag.Name) : value.AsString()Additionally, more research must be done to determine the behavioral differences for the following methods with their accepted value types and the exceptions they throw.
Format(Type enumType, object value, string format)GetName(Type enumType, object value)IsDefined(Type enumType, object value)Other potential breaking changes if corefx#15453 gets implemented
EnumsNET.PrimaryEnumMemberAttributetoSystem.ComponentModel.PrimaryAttribute.System.ComponentModel.AttributeCollectionand then add appropriate extension methods to support the existing methods onEnumsNET.AttributeCollectionin older versions ofSystem.ComponentModel.AttributeCollection. This will mean in older framework targets the attribute collection will not explicitly implementIList<Attribute>orIReadOnlyList<Attribute>as is currently the case but will support being casted to those interfaces as the returned attribute collection will derive fromSystem.ComponentModel.AttributeCollectionfor older framework targets.GetAllFlagstoAllFlagsas in the proposal.IsValidFlagCombinationwill be promoted to an extension method in older framework targets.EnumMemberin newer framework targets that already contain the built-inEnumMemberand use that instead.