SmartEnum
SmartEnum copied to clipboard
Cast of non-existing Combination wrongly return the item with lowest value
The cast of non-existing Combination value1 | value2
to TestFlagEnumWithoutCombination
wrongly returns the item with the lowest internal value (i.e. value1
).
This is totally misleading and error-prone, moreover is not consistent with .net enums' behavior.
I think it should probably return a special runtime object (similarly to the "collection" of enum values) or just throw an exception.
using Ardalis.SmartEnum;
{
var value1and2_smartEnum = (TestSmartFlagEnum)(TestSmartFlagEnum.Value1 | TestSmartFlagEnum.Value2);
var value1and2_dotnetEnum = TestFlagEnum.Value1 | TestFlagEnum.Value2;
Console.WriteLine($"{value1and2_smartEnum} == {value1and2_dotnetEnum}");
// prints Value1and2 == Value1and2
}
{
var value1and2_smartEnum = (TestSmartFlagEnumWithoutCombination)(TestSmartFlagEnumWithoutCombination.Value1 | TestSmartFlagEnumWithoutCombination.Value2);
var value1and2_dotnetEnum = TestFlagEnumWithoutCombination.Value1 | TestFlagEnumWithoutCombination.Value2;
Console.WriteLine($"{value1and2_smartEnum} != {value1and2_dotnetEnum}");
// prints Value1 != Value1, Value2
}
[Flags]
public enum TestFlagEnum
{
Value1 = 1,
Value2 = 2,
// Combinations
Value1and2 = 3
}
public class TestSmartFlagEnum : SmartFlagEnum<TestSmartFlagEnum>
{
public static readonly TestSmartFlagEnum Value1 = new(nameof(Value1), 1);
public static readonly TestSmartFlagEnum Value2 = new(nameof(Value2), 2);
// Combinations
public static readonly TestSmartFlagEnum Value1and2 = new(nameof(Value1and2), 3);
public TestSmartFlagEnum(string name, int value) : base(name, value) { }
}
[Flags]
public enum TestFlagEnumWithoutCombination
{
Value1 = 1,
Value2 = 2
}
public class TestSmartFlagEnumWithoutCombination : SmartFlagEnum<TestSmartFlagEnumWithoutCombination>
{
public static readonly TestSmartFlagEnumWithoutCombination Value1 = new(nameof(Value1), 1);
public static readonly TestSmartFlagEnumWithoutCombination Value2 = new(nameof(Value2), 2);
public TestSmartFlagEnumWithoutCombination(string name, int value) : base(name, value) { }
}