Skip to content

Instantly share code, notes, and snippets.

@ryanwebjackson
Created November 3, 2017 16:56
Show Gist options
  • Save ryanwebjackson/f95b0a28b51295e2a9f49019b68440dd to your computer and use it in GitHub Desktop.
Save ryanwebjackson/f95b0a28b51295e2a9f49019b68440dd to your computer and use it in GitHub Desktop.
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