|
| 1 | +/** |
| 2 | + * $File: JCS_Enum.cs $ |
| 3 | + * $Date: $ |
| 4 | + * $Revision: $ |
| 5 | + * $Creator: Jen-Chieh Shen $ |
| 6 | + * $Notice: See LICENSE.txt for modification and distribution information |
| 7 | + * Copyright (c) 2025 by Shen, Jen-Chieh $ |
| 8 | + */ |
| 9 | +using System; |
| 10 | +using System.Collections.Generic; |
| 11 | +using System.Linq; |
| 12 | + |
| 13 | +namespace JCSUnity |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Enumerator utilities. |
| 17 | + /// </summary> |
| 18 | + public static class JCS_Enum |
| 19 | + { |
| 20 | + /* Variables */ |
| 21 | + |
| 22 | + /* Setter & Getter */ |
| 23 | + |
| 24 | + /* Functions */ |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Enum typed version casting. |
| 28 | + /// Source: http://stackoverflow.com/questions/972307/can-you-loop-through-all-enum-values |
| 29 | + /// </summary> |
| 30 | + public static IEnumerable<T> GetValues<T>() |
| 31 | + { |
| 32 | + return Enum.GetValues(typeof(T)).Cast<T>(); |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Convert enum values to array. |
| 37 | + /// </summary> |
| 38 | + public static ICollection<T> ToArray<T>() |
| 39 | + { |
| 40 | + return GetValues<T>().ToArray(); |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Convert enum values to list. |
| 45 | + /// </summary> |
| 46 | + public static ICollection<T> ToList<T>() |
| 47 | + { |
| 48 | + return GetValues<T>().ToList(); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Retrieves an array of the names of the constants |
| 53 | + /// in a specified enumeration. |
| 54 | + /// </summary> |
| 55 | + public static string[] GetNames<T>() |
| 56 | + { |
| 57 | + return Enum.GetNames(typeof(T)); |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Return the length of an enumerator. |
| 62 | + /// </summary> |
| 63 | + /// <typeparam name="T"> Enum type. </typeparam> |
| 64 | + /// <returns> Size of the enum listed. </returns> |
| 65 | + public static int Size<T>() |
| 66 | + { |
| 67 | + return GetNames<T>().Length; |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments