Created
July 20, 2015 01:17
-
-
Save emoacht/0bf8d42ec304914d3cc4 to your computer and use it in GitHub Desktop.
Get Enum values from Type variable.
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 EnumService | |
{ | |
public enum Hero | |
{ | |
Super, | |
Bat, | |
Iron, | |
} | |
public void ShowEnumValues() | |
{ | |
PrintTypeAndString(EnumerateEnumValues1(typeof(Hero))); | |
PrintTypeAndString(EnumerateEnumValues2(typeof(Hero))); | |
PrintTypeAndString(EnumerateEnumValues3(typeof(Hero))); | |
PrintTypeAndString(EnumerateEnumValues4(typeof(Hero))); | |
PrintTypeAndString(EnumerateEnumValues5(typeof(Hero))); | |
// ---------- | |
// Type: EnumSample.EnumService+Hero, String: Super | |
// Type: EnumSample.EnumService+Hero, String: Bat | |
// Type: EnumSample.EnumService+Hero, String: Iron | |
// ---------- | |
// Type: EnumSample.EnumService+Hero, String: Super | |
// Type: EnumSample.EnumService+Hero, String: Bat | |
// Type: EnumSample.EnumService+Hero, String: Iron | |
// ---------- | |
// Type: EnumSample.EnumService+Hero, String: Super | |
// Type: EnumSample.EnumService+Hero, String: Bat | |
// Type: EnumSample.EnumService+Hero, String: Iron | |
// ---------- | |
// Type: EnumSample.EnumService+Hero, String: Super | |
// Type: EnumSample.EnumService+Hero, String: Bat | |
// Type: EnumSample.EnumService+Hero, String: Iron | |
// ---------- | |
// Type: EnumSample.EnumService+Hero, String: Super | |
// Type: EnumSample.EnumService+Hero, String: Bat | |
// Type: EnumSample.EnumService+Hero, String: Iron | |
} | |
private void PrintTypeAndString(IEnumerable<object> elements) | |
{ | |
Debug.WriteLine("----------"); | |
foreach (var element in elements) | |
Debug.WriteLine("Type: {0}, String: {1}", element.GetType(), element); | |
} | |
public IEnumerable<Enum> EnumerateEnumValues1(Type enumType) | |
{ | |
if (!enumType.IsEnum) | |
throw new ArgumentException("enumType"); | |
return Enum.GetNames(enumType) | |
.Select(x => Enum.Parse(enumType, x) as Enum) | |
.Where(x => x != null); | |
} | |
public IEnumerable<Enum> EnumerateEnumValues2(Type enumType) | |
{ | |
if (!enumType.IsEnum) | |
throw new ArgumentException("enumType"); | |
return Enum.GetNames(enumType) | |
.Select(x => (Enum)Enum.Parse(enumType, x)); | |
} | |
public IEnumerable<Enum> EnumerateEnumValues3(Type enumType) | |
{ | |
if (!enumType.IsEnum) | |
throw new ArgumentException("enumType"); | |
return Enum.GetValues(enumType) | |
.Cast<object>() | |
.Select(x => x as Enum) | |
.Where(x => x != null); | |
} | |
public IEnumerable<Enum> EnumerateEnumValues4(Type enumType) | |
{ | |
if (!enumType.IsEnum) | |
throw new ArgumentException("enumType"); | |
return Enum.GetValues(enumType) | |
.Cast<object>() | |
.Select(x => (Enum)x); | |
} | |
public IEnumerable<Enum> EnumerateEnumValues5(Type enumType) | |
{ | |
if (!enumType.IsEnum) | |
throw new ArgumentException("enumType"); | |
return Enum.GetValues(enumType) | |
.Cast<Enum>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment