Created
January 3, 2020 11:19
-
-
Save garpunkal/bb096f4744d066d9a6fef0bef1aaf36e to your computer and use it in GitHub Desktop.
EnumHelpers.cs
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.ComponentModel; | |
using System.Linq; | |
using System.Reflection; | |
namespace Helpers | |
{ | |
public class EnumHelpers | |
{ | |
public static string GetEnumDescription(Enum value) | |
{ | |
FieldInfo fi = value.GetType().GetField(value.ToString()); | |
DescriptionAttribute[] attributes = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[]; | |
return (attributes != null && attributes.Length > 0) ? attributes[0].Description : value.ToString(); | |
} | |
public static string GetDescription(Enum enumValue) | |
{ | |
string output = null; | |
Type type = enumValue.GetType(); | |
FieldInfo fi = type.GetField(enumValue.ToString()); | |
var attrs = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[]; | |
if (attrs.Length > 0) output = attrs[0].Description; | |
return (!string.IsNullOrWhiteSpace(output)) ? output : enumValue.ToString(); | |
} | |
public static T GetEnumValue<T>(int id) | |
{ | |
Type enumType = typeof(T); | |
// Can't use type constraints on value types, so have to do check like this | |
if (enumType.BaseType != typeof(Enum)) | |
throw new ArgumentException("T must be of type System.Enum"); | |
if (Enum.IsDefined(enumType, id)) | |
return (T)Enum.ToObject(enumType, id); | |
else if (Enum.IsDefined(enumType, id.ToString())) | |
return (T)Enum.Parse(enumType, id.ToString()); | |
else | |
throw new ArgumentException(string.Format("Parameter id \"{0}\" is not defined in {1}", id.ToString(), enumType)); | |
} | |
public static T GetEnumValue<T>(string value) | |
{ | |
Type enumType = typeof(T); | |
int attemptedValue = 0; | |
// Can't use type constraints on value types, so have to do check like this | |
if (enumType.BaseType != typeof(Enum)) | |
throw new ArgumentException("T must be of type System.Enum"); | |
if (int.TryParse(value, out attemptedValue) && Enum.IsDefined(enumType, attemptedValue)) | |
return (T)Enum.Parse(enumType, value); | |
else if (Enum.IsDefined(enumType, value)) | |
return (T)Enum.Parse(enumType, value); | |
else | |
throw new ArgumentException(string.Format("Parameter for value \"{0}\" is not defined in {1}", value, enumType)); | |
} | |
public static IDictionary<T, string> GetEnumValuesWithDescription<T>(Type type) where T : struct, IConvertible | |
{ | |
if (!type.IsEnum) | |
throw new ArgumentException("T must be an enumerated type"); | |
return type.GetEnumValues().OfType<T>().ToDictionary(key => key, val => GetDescription(val as Enum)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment