| | 1 | | using System.ComponentModel; |
| | 2 | |
|
| | 3 | | namespace Syki.Shared; |
| | 4 | |
|
| | 5 | | public static class EnumExtensions |
| | 6 | | { |
| | 7 | | public static string GetDescription(this Enum value) |
| | 8 | | { |
| 15385 | 9 | | if (value == null) |
| | 10 | | { |
| 1 | 11 | | return string.Empty; |
| | 12 | | } |
| | 13 | |
|
| 15384 | 14 | | var attribute = value.GetType() |
| 15384 | 15 | | .GetField(value.ToString())! |
| 15384 | 16 | | .GetCustomAttributes(typeof(DescriptionAttribute), inherit: false); |
| | 17 | |
|
| 15384 | 18 | | if (attribute is DescriptionAttribute[] source && source.Length != 0) |
| | 19 | | { |
| 15383 | 20 | | return source.First().Description; |
| | 21 | | } |
| | 22 | |
|
| 1 | 23 | | return value.ToString(); |
| | 24 | | } |
| | 25 | |
|
| | 26 | | public static bool IsIn(this Enum source, params Enum[] valuesToCheck) |
| | 27 | | { |
| 12 | 28 | | if (valuesToCheck == null || valuesToCheck.Length == 0) |
| | 29 | | { |
| 2 | 30 | | return false; |
| | 31 | | } |
| | 32 | |
|
| 10 | 33 | | return valuesToCheck.Contains(source); |
| | 34 | | } |
| | 35 | |
|
| | 36 | | public static T ToEnum<T>(this string value) |
| | 37 | | { |
| 5498 | 38 | | return (T)Enum.Parse(typeof(T), value, true); |
| | 39 | | } |
| | 40 | |
|
| | 41 | | public static bool IsValid(this Enum value) |
| | 42 | | { |
| 1089 | 43 | | return Enum.IsDefined(value.GetType(), value); |
| | 44 | | } |
| | 45 | |
|
| | 46 | | public static bool Is(this DayOfWeek dayOfWeek, Day day) |
| | 47 | | { |
| 30292 | 48 | | return day.ToString() == dayOfWeek.ToString(); |
| | 49 | | } |
| | 50 | |
|
| | 51 | | public static int DiffInMinutes(this Hour hourA, Hour hourB) |
| | 52 | | { |
| 4336 | 53 | | if (hourA > hourB) |
| | 54 | | { |
| 4 | 55 | | (hourA, hourB) = (hourB, hourA); |
| | 56 | | } |
| | 57 | |
|
| 4336 | 58 | | var hourAHour = int.Parse(hourA.ToString().Substring(1, 2)); |
| 4336 | 59 | | var hourAMinute = int.Parse(hourA.ToString().Substring(4, 2)); |
| | 60 | |
|
| 4336 | 61 | | var hourBHour = int.Parse(hourB.ToString().Substring(1, 2)); |
| 4336 | 62 | | var hourBMinute = int.Parse(hourB.ToString().Substring(4, 2)); |
| | 63 | |
|
| 4336 | 64 | | var hours = hourBHour - hourAHour; |
| 4336 | 65 | | var minutes = hourBMinute - hourAMinute; |
| | 66 | |
|
| 4336 | 67 | | return hours * 60 + minutes; |
| | 68 | | } |
| | 69 | | } |