EnumExtension with contains
01/09/2020
Aucun commentaire
For simplify flag enum usage
use this code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
public static class EnumExtension { /// <summary> /// convert enum value to int value /// </summary> /// <typeparam name="T">T must be an Enum</typeparam> /// <param name="soure">Enum of type T</param> /// <returns>int value of enum</returns> public static int ToInt<T>(this T soure) where T : IConvertible//enum { if (!typeof(T).IsEnum) throw new ArgumentException("T must be an enumerated type"); return (int)(IConvertible)soure; } /// <summary> /// return the length of the enum of type T /// </summary> /// <typeparam name="T">T must be an enum</typeparam> /// <param name="soure">Enum of type T</param> /// <returns>the length of the enum enter as parameter</returns> public static int Count<T>(this T soure) where T : IConvertible//enum { if (!typeof(T).IsEnum) throw new ArgumentException("T must be an enumerated type"); return Enum.GetNames(typeof(T)).Length; } /// <summary> /// verify if an enum 'e' contain the element 's' and return true or false /// </summary> /// <typeparam name="T">T must be an Iconvertible</typeparam> /// <param name="e">Enum of type</param> /// <param name="s">Searched object of type T</param> /// <returns>true if 'e' contains 's' or false</returns> public static bool Contains<T>(this T e, params T[] ss) where T : IConvertible//enum { if (!typeof(T).IsEnum) throw new ArgumentException("T must be an enumerated type"); foreach (var s in ss) { if ((e.ToInt() & s.ToInt()) == s.ToInt()) return true; } return false; } #region Version for single contains //public static bool Contains<T>(this T e, T s) where T : IConvertible//enum //{ // if (!typeof(T).IsEnum) // throw new ArgumentException("T must be an enumerated type"); // return (e.ToInt() & s.ToInt()) == s.ToInt(); //} #endregion } |
and usage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
[Flags] public enum RunCheckEnum { CheckRelativInitialize = 0x0001, CheckInitialize = 0x0002, CheckFunction = 0x0004, CheckAll = CheckRelativInitialize | CheckInitialize | CheckFunction, ModeCheckStack = RunRelativInitialize | RunInitialize | RunOneFunction, RunRelativInitialize = 0x0010, RunInitialize = 0x0020, RunOneFunction = 0x0040, RunAllFunction = 0x0080, RunAll = 0x00f0, } { RunCheckEnum rce = RunCheckEnum.CheckRelativInitialize; bool a = rce.Contains(RunCheckEnum.CheckRelativInitialize); bool b = rce.Contains(RunCheckEnum.RunRelativInitialize); bool c = rce.Contains(RunCheckEnum.CheckRelativInitialize, RunCheckEnum.RunRelativInitialize); } { RunCheckEnum rce = RunCheckEnum.RunRelativInitialize; bool a = rce.Contains(RunCheckEnum.CheckRelativInitialize); bool b = rce.Contains(RunCheckEnum.RunRelativInitialize); bool c = rce.Contains(RunCheckEnum.CheckRelativInitialize, RunCheckEnum.RunRelativInitialize); } |
Categories: Non classé
Commentaires récents