For Enum.GetValues and GetNames , the elements of each array are sorted by the binary values.
Therefore, continuity checks should be performed after casting to unsigned, and minimum and maximum value searches can be easily performed on this basis. (If there is no value cover, the configuration of s_orderedMembers is also not necessary.)
|
static bool isContinuous(int uniqueCount, T max, T min) |
|
{ |
|
if (uniqueCount <= 0) |
|
return false; |
|
|
|
var length = toUInt64(max) - toUInt64(min); |
|
var count = (ulong)uniqueCount - 1; |
|
return length == count; |
|
} |
|
|
|
|
|
static ulong toUInt64(T value) |
|
{ |
|
return s_typeCode switch |
|
{ |
|
TypeCode.SByte => (ulong)Unsafe.BitCast<T, sbyte>(value), |
|
TypeCode.Byte => Unsafe.BitCast<T, byte>(value), |
|
TypeCode.Int16 => (ulong)Unsafe.BitCast<T, short>(value), |
|
TypeCode.UInt16 => Unsafe.BitCast<T, ushort>(value), |
|
TypeCode.Int32 => (ulong)Unsafe.BitCast<T, int>(value), |
|
TypeCode.UInt32 => Unsafe.BitCast<T, uint>(value), |
|
TypeCode.Int64 => (ulong)Unsafe.BitCast<T, long>(value), |
|
TypeCode.UInt64 => Unsafe.BitCast<T, ulong>(value), |
|
_ => throw new InvalidOperationException(), |
|
}; |
For Enum.GetValues and GetNames , the elements of each array are sorted by the binary values.
Therefore,
continuity checks should be performed after casting to unsigned, and minimum and maximum value searches can be easily performed on this basis. (If there is no value cover, the configuration of s_orderedMembers is also not necessary.)FastEnum/src/libs/FastEnum.Core/Internals/EnumInfo.cs
Lines 61 to 85 in db9a379