ObjectEnum 1.0.0-alpha
See the version list below for details.
dotnet add package ObjectEnum --version 1.0.0-alpha
NuGet\Install-Package ObjectEnum -Version 1.0.0-alpha
<PackageReference Include="ObjectEnum" Version="1.0.0-alpha" />
paket add ObjectEnum --version 1.0.0-alpha
#r "nuget: ObjectEnum, 1.0.0-alpha"
// Install ObjectEnum as a Cake Addin #addin nuget:?package=ObjectEnum&version=1.0.0-alpha&prerelease // Install ObjectEnum as a Cake Tool #tool nuget:?package=ObjectEnum&version=1.0.0-alpha&prerelease
ObjectEnum
ObjectEnum is a wrapper for enums, adding a certain degree of polymorphism.
Examples
Restrict enum values to a subset
Conssider the WorkDay class:
public class WorkDay : ObjectEnum<DayOfWeek>
{
public WorkDay(DayOfWeek value)
: base(value) { }
private static readonly DayOfWeek[] Values = new[]
{
DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday,
DayOfWeek.Thursday, DayOfWeek.Friday
};
protected override IReadOnlyCollection<DayOfWeek> GetDefinedValues()
=> Values;
}
Which will produce the following results:
var sunday = new WorkDay(DayOfWeek.Sunday); //throws exception
var monday = new WorkDay(DayOfWeek.Monday); //works fine
var label = $"{monday} is day {(int)monday}." //produces: "Monday is day 1."
var mondayIsAlwaysMonday = monday == DayOfWeek.Monday; //true, sorry...
Inheritance
Conssider these classes:
public class ChillDay : ObjectEnum<DayOfWeek>
{
public ChillDay(DayOfWeek value)
: base(value) { }
private static readonly DayOfWeek[] Values = new[]
{
DayOfWeek.Saturday, DayOfWeek.Sunday
};
protected override IReadOnlyCollection<DayOfWeek> GetDefinedValues()
=> Values;
}
public class FunDay : ChillDay
{
public FunDay(DayOfWeek value)
: base(value) { }
private static readonly DayOfWeek[] Values = new[]
{
DayOfWeek.Saturday, DayOfWeek.Sunday, DayOfWeek.Friday
};
protected override IReadOnlyCollection<DayOfWeek> GetDefinedValues()
=> Values;
}
Which will produce the following results:
var sunday = new ChillDay(DayOfWeek.Sunday);
var funday = new FundDay(DayOfWeek.Sunday);
var sundayIsFunday = sunday == funday; //true
In this case the comparison will return true, because FundDay inherits ChillDay. However in the case of a Workday, this would not be the case as there is no relation between a WorkDay and a FundDay.
var friday = new WorkDay(DayOfWeek.Friday);
var funday = new FundDay(DayOfWeek.Friday);
var workFridayIsNoFunFriday = friday == funday; //false
Switch cases
You can easily use a ObjectEnum in a switch case, however you will need to cast it to the appropriate enum.
var friday = new WorkDay(DayOfWeek.Friday);
switch((DayOfWeek)friday){
case DayOfWeek.Monday:
//do something monday related
break;
/*...*/
case DayOfWeek.Friday:
//do something friday related
break;
}
Or cast it to an integer.
var friday = new WorkDay(DayOfWeek.Friday);
switch((int)friday){
case 1:
//do something monday related
break;
/*...*/
case 5:
//do something friday related
break;
}
Declaring your own ObjectEnums
//you can optionally declare an abstract base type for your implementations
public abstract class Size : ObjectEnum<Size.Value>
{
//define a single enum containing all possible values
//it can be a nested enum like it is here,
//but this is not required
public enum Value
{
Unknown = 0,
ExtraSmall = 1,
Small = 2,
Medium = 3,
Large = 4,
ExtraLarge = 5
}
protected Size(Value value)
: base(value) { }
}
public class BasicSize : Size
{
public BasicSize(Value value)
: base(value) { }
//declaring a static Values array reduces
//the overhead on creating a new array each time
//we instantiate a BasicSize object
private static readonly Value[] Values = new[]
{
Value.Unknown, Value.Small, Value.Medium, Value.Large
};
//override GetDefinedValues to restrict the possible values for BasicSize
protected override IReadOnlyCollection<Value> GetDefinedValues()
=> Values;
}
//inherit BasicSize so we can compare the values
public class ExtendedSize : BasicSize
{
public ExtendedSize(Value value)
: base(value) { }
//not the prefered way, but should work
protected override IReadOnlyCollection<Value> GetDefinedValues()
=> new[] {
Value.Unknown, Value.ExtraSmall, Value.Small, Value.Medium, Value.Large, Value.ExtraLarge
};
}
Other helpful stuff
As you may have noticed, it is easy to cast between a ObjectEnum and its underlying enum Type, as well as casting to the int representation.
Besides this, a number of helper methods are defined to help you instantiate ObjectEnums
var isFriday = WorkDay.TryParse("Friday", out var friday); //returns true, friday = new WorkDay(DayOfWeek.Friday)
var isMonday = WorkDay.TryParse("monday", ignoreCase: false, out var mondaytues); //returns true, monday = new WorkDay(DayOfWeek.Monday)
var tuesday = WorkDay.Parse<WorkDay>("tuesday"); //returns new WorkDay(DayOfWeek.Tuesday)
var wednessday = WorkDay.Create<WorkDay>(DayOfWeek.Wednessday); //returns new WorkDay(DayOfWeek.Wednessday)
You can also use the various overloads of IsDefined
to determine whether a certain value is valid for the given ObjectEnum.
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
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 587 | 11/15/2019 |
1.0.0-alpha1 | 388 | 10/15/2019 |
1.0.0-alpha | 390 | 10/15/2019 |
This first version was hacked together in a few hours because I couldn't quite find an elegant solution for polymorphic enums on the net. Things got out of hand, and I ended up with a pretty feature complete package, so here you go.
Please keep in mind that this solution has not been field tested, and quite frankly, I was a bit drunk when I wrote it. Please refer to the GitHub repo for documentation as well as any issues, comments or requests.