Let's look at an extremely simple Enum.
/// <Summary> // Chinese English // </summary> enum Chinglish {Niubility, Zhuangbility, Shability, Erbility ,}
(In an episode: the above four enum values for Chinese English come from --
Registrant people think they are fullNiubilityAnd like to playZhuangbility, Which only reflect theirShabilityAndErbility.
For Chinese meanings, google)
Original Enum Traversal
In general, if we want to traverse the above Enum, one of the methods is:
Array values = Enum.GetValues(typeof(Chinglish )); foreach (var value in values){ Chinglish e =(Chinglish)value; //do sth. ...}
I don't know if there is a simpler way of writing, but if every time I traverse an Enum type, I will write it like above, it's a bit...
There are two points that are not beautiful:
1. typeof (Chinglish)
While Enum. GetValues (typeof (Chinglish); returns the Array type and needs to be cast again.
2. Chinglish e = (Chinglish) value;
That is to say, type conversion is required before you can proceed to the next step.
Enum Foreach Encapsulation
These two indecent points prompted me to encapsulate it and finally get the following code:
Enum<Chinglish>.Foreach(e => Console.WriteLine(e));
Implementation is simple:
public class Enum<T>{ public static void Foreach(Action<T> action) { Array values = Enum.GetValues(typeof(T)); foreach (var value in values) { action((T)value); } }}
This Enum <T>. Foreach method has helped me a lot, so that I can traverse Enum with a single code. Later, I found that Foreach alone is not enough. Sometimes I want to query Enum and naturally want to add a Where method similar to Linq for it:
public static IEnumerable<T> Where(Func<T, bool> func)
Since the Where clause is available, the Single clause should not be missing:
public static T Single(Func<T, bool> func)
......
Let Enum Enumerable
This kind of "greed" will be infinitely expanded, So I simply don't have to do it any more. I directly come to a AsEnumerable to let it return an IEnumerable <T> type, so I can do everything I want, where, Single, and Foreach:
//Let Enum Enumerablevar en = Enum<Chinglish>.AsEnumerable();//Printen.ForEach(e => Console.WriteLine(e));//Call other test methoden.ForEach(e => EnumMethod(e));//Filtervar filter = en.Where(c => (int)c > 2);
Summary
I need to traverse enum in an application or perform more complex operations on it. Generally, there are two situations:
1. Test code
For example, en. ForEach (e =>EnumMethod(E); Whether to overwrite all conditions in EnumMethod.
2. do other things with other extensions of enum.
Complete source code:
public class Enum<T>{ public static IEnumerable<T> AsEnumerable() { EnumQuery<T> query = new EnumQuery<T>(); return query; }}class EnumQuery<T> : IEnumerable<T>{ private List<T> list; #region IEnumerable<T> Members public IEnumerator<T> GetEnumerator() { Array values = Enum.GetValues(typeof(T)); list = new List<T>(values.Length); foreach (var value in values) { list.Add((T)value); } return list.GetEnumerator(); } #endregion #region IEnumerable Members System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); } #endregion}