EnhancedEnum-StrongNamed
1.21.1.2310
dotnet add package EnhancedEnum-StrongNamed --version 1.21.1.2310
NuGet\Install-Package EnhancedEnum-StrongNamed -Version 1.21.1.2310
<PackageReference Include="EnhancedEnum-StrongNamed" Version="1.21.1.2310" />
paket add EnhancedEnum-StrongNamed --version 1.21.1.2310
#r "nuget: EnhancedEnum-StrongNamed, 1.21.1.2310"
// Install EnhancedEnum-StrongNamed as a Cake Addin #addin nuget:?package=EnhancedEnum-StrongNamed&version=1.21.1.2310 // Install EnhancedEnum-StrongNamed as a Cake Tool #tool nuget:?package=EnhancedEnum-StrongNamed&version=1.21.1.2310
EnhancedEnum
Enumeration that auto converts to string, integer, as well as provides easy looping through the options.
Feature Overview
- Simple Assignments
- Auto Data conversion
- Configurable underlying data types
- Configurable error handling
Comparison of a regular enum and EnhancedEnum - Example
// Assignments
RegularEnum regularEnum = RegularEnum.Running;
StatusEnum enhancedEnum = StatusEnum.Running;
FlagsEnum flagsEnum = FlagsEnum.Four;
string sRegular = RegularEnum.Running.ToString();
string sEnhanced = StatusEnum.Running;
string sFlags = FlagsEnum.Four;
int iRegular = (int)RegularEnum.Running;
int iEnhanced = StatusEnum.Running;
int iFlags = FlagsEnum.Four;
// String to Enum
regularEnum = (RegularEnum)Enum.Parse(typeof(RegularEnum), sRegular);
regularEnum = Enum.Parse<RegularEnum>(sRegular);
enhancedEnum = sEnhanced;
flagsEnum = sFlags;
// Int to Enum
regularEnum = (RegularEnum) iRegular;
enhancedEnum = iEnhanced;
flagsEnum = iFlags;
// Flag Manipulation
regularEnum = RegularEnum.Stopped | RegularEnum.Error;
flagsEnum = FlagsEnum.Four | FlagsEnum.Eight;
regularEnum.HasFlag(RegularEnum.Stopped);
flagsEnum.HasFlag(FlagsEnum.Four);
// regularEnum.HasFlag(2); // Compile Error
flagsEnum.HasFlag(2); // Valid check
// Errors on conversion: Configurable to be NULL / throw exception.
regularEnum = (RegularEnum) 55; // Assigns 55
regularEnum = (RegularEnum)Enum.Parse(typeof(RegularEnum), "Running1"); // throws ArgumentException
enhancedEnum = 55; // returns null;
enhancedEnum = "Running1"; // returns null;
StatusEnum.ThrowOnError = true;
enhancedEnum = 55; // throws InvalidOperationException
enhancedEnum = "Running1"; // throws InvalidOperationException
Feature Examples
Auto data type conversion
string t = StatusEnum.Running;
StatusEnum t2 = StatusEnum.Stopped;
int t3 = StatusEnum.Error;
IEnumerable<StatusEnum> vals = StatusEnum.Values;
StatusEnum t4 = t3;
StatusEnum t5 = t;
Flags
var t = FlagsEnum.One | FlagsEnum.Two;
t.HasFlag(FlagsEnum.Two); // Returns True
t.HasFlag(FlagsEnum.Four); // Returns False
Non Integer value enums
var nie = NonIntEnum.December;
DateTime d1 = NonIntEnum.December;
string s1 = NonIntEnum.December;
NonIntEnum nie4 = "December 1st";
NonIntEnum nie5 = DateTime.Parse("12/1/2020");
Usage - Simple Enum
public sealed class StatusTest : EnhancedEnum<int, StatusTest>
{
[Description("Indicates Running")] // Description Property.
[DisplayName("In Process")] // Displays this text when converted to a string.
[Value(5)] // Underlying value.
public static readonly StatusTest Running = new StatusTest();
// Gets the default value of 2 because it is the second one in the list. Can use ValueAttribute to override.
public static readonly StatusTest Stopped = new StatusTest();
// Gets the default value of 3 because it is the third one in the list. Can use ValueAttribute to override.
public static readonly StatusTest Error = new StatusTest();
// Operators allow conversion to this class from other values.
public static implicit operator StatusTest(string value) => Convert(value);
public static implicit operator StatusTest(int value) => Convert(value);
}
Usage - Flags Enum
[Flag]
public sealed class FlagsEnum : EnhancedEnum<int, FlagsEnum>
{
[Value(0)]
public static readonly FlagsEnum None = new FlagsEnum();
[Value(1)]
public static readonly FlagsEnum One = new FlagsEnum();
[Value(2)]
public static readonly FlagsEnum Two = new FlagsEnum();
[Value(4)]
public static readonly FlagsEnum Four = new FlagsEnum();
[Value(8)]
public static readonly FlagsEnum Eight = new FlagsEnum();
// This allows us to assign back to this class.
public static implicit operator FlagsEnum(string value) => Convert(value);
public static implicit operator FlagsEnum(int value) => Convert(value);
protected override FlagsEnum GetValue(int value) => Convert(value);
}
Usage - Non Integer Value Enum
public sealed class NonIntEnum : EnhancedEnum<DateTime, NonIntEnum>
{
[Description("Indicates Month of December")]
[DisplayName("December 1st")]
[Value("12/1/2020")]
public static readonly NonIntEnum December = new NonIntEnum();
[Value("11/1/2020")]
public static readonly NonIntEnum November = new NonIntEnum();
[Value("1/1/2020")]
public static readonly NonIntEnum January = new NonIntEnum();
// This allows us to assign back to this class.
public static implicit operator NonIntEnum(string value) => Convert(value);
public static implicit operator NonIntEnum(DateTime value) => Convert(value);
// Custom value type converter
protected override DateTime TypeConverter(object value) => DateTime.Parse(value.ToString());
}
License
MIT License
Copyright (c) 2020 cwinland
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on EnhancedEnum-StrongNamed:
Package | Downloads |
---|---|
Cron.Core.StrongNamed
Cron builder object that can be used to build Cron expressions, describe them, and manipulate objects. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated | |
---|---|---|---|
1.21.1.2310 | 4,963 | 1/23/2021 | |
1.21.1.1017 | 900 | 1/10/2021 | |
1.20.11.2220 | 1,054 | 11/22/2020 | |
1.20.11.1812 | 880 | 11/18/2020 | |
1.1.11.15 | 1,130 | 11/15/2020 | |
1.1.11.14 | 1,097 | 11/14/2020 |