Created
November 3, 2017 16:56
-
-
Save ryanwebjackson/f95b0a28b51295e2a9f49019b68440dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EasyEnum<T> : IEnumerable<object> | |
{ | |
private List<object> _values; | |
private Type underlyingType; | |
public EasyEnum() | |
{ | |
if (typeof(T).IsSubclassOf(typeof(Enum))) | |
{ | |
this.underlyingType = Enum.GetUnderlyingType(typeof(T)); | |
this._values = new List<object>(); | |
foreach (var value in Enum.GetValues(typeof(T))) | |
{ | |
if (value.GetType() != this.underlyingType) | |
{ | |
throw new ArgumentException("Unexpected enum value type: " + value.GetType()); | |
} | |
this._values.Add(value); | |
} | |
} | |
else | |
{ | |
throw new ArgumentException("Must provide enum type as generic type parameter."); | |
} | |
} | |
public IEnumerator<object> GetEnumerator() | |
{ | |
return this.GetEnumerator(); | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return this._values.GetEnumerator(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment