Last active
April 20, 2016 19:42
-
-
Save jessestricker/8d0b9cf85f759d79b03b to your computer and use it in GitHub Desktop.
[C#] A generic util class for enums.
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
public static class Enum<T> where T : struct | |
{ | |
public static IEnumerable<EnumValue<T>> Values => | |
from object value in Enum.GetValues(typeof(T)) | |
select new EnumValue<T>((T)value); | |
public static IEnumerable<string> Names => | |
Enum.GetNames(typeof(T)); | |
public static string GetName(T value) => | |
Enum.GetName(typeof(T), value); | |
} | |
public class EnumValue<T> where T : struct | |
{ | |
public EnumValue(T value) | |
{ | |
Value = value; | |
} | |
public T Value { get; } | |
public string Name => Enum<T>.GetName(Value); | |
public static implicit operator T(EnumValue<T> v) => v.Value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment