How to Get C# Enum Description from Value

How to get C# Enum description from value?


int value = 1;
string description = Enumerations.GetEnumDescription((MyEnum)value);

The default underlying data type for an enum in C# is an int, you can just cast it.

Get Description Attributes From a Flagged Enum

HasFlag is your friend. :-)

The extension method below uses the GetDescription extension method you've posted above, so ensure you have that. The following should then work:

public static List<string> GetDescriptionsAsText(this Enum yourEnum)
{
List<string> descriptions = new List<string>();

foreach (Enum enumValue in Enum.GetValues(yourEnum.GetType()))
{
if (yourEnum.HasFlag(enumValue))
{
descriptions.Add(enumValue.GetDescription());
}
}

return descriptions;
}

Note: HasFlag allows you to compare a given Enum value against the flags defined. In your example, if you have

Result y = Result.Value1 | Result.Value2 | Result.Value4;

then

y.HasFlag(Result.Value1)

should be true, while

y.HasFlag(Result.Value3)

will be false.

See also: https://msdn.microsoft.com/en-us/library/system.enum.hasflag(v=vs.110).aspx

How to get the DescriptionAttribute value from an enum member

Use this

private string GetEnumDescription(Enum value)
{
// Get the Description attribute value for the enum value
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

if (attributes.Length > 0)
return attributes[0].Description;
else
return value.ToString();
}

Get Enum from Description attribute


public static class EnumEx
{
public static T GetValueFromDescription<T>(string description) where T : Enum
{
foreach(var field in typeof(T).GetFields())
{
if (Attribute.GetCustomAttribute(field,
typeof(DescriptionAttribute)) is DescriptionAttribute attribute)
{
if (attribute.Description == description)
return (T)field.GetValue(null);
}
else
{
if (field.Name == description)
return (T)field.GetValue(null);
}
}

throw new ArgumentException("Not found.", nameof(description));
// Or return default(T);
}
}

Usage:

var panda = EnumEx.GetValueFromDescription<Animal>("Giant Panda");

Enum String Name from Value

You can convert the int back to an enumeration member with a simple cast, and then call ToString():

int value = GetValueFromDb();
var enumDisplayStatus = (EnumDisplayStatus)value;
string stringValue = enumDisplayStatus.ToString();

Getting Description Attribute of a Generic Enum

Here, try this:

public static KeyValuePair<string, List<KeyValueDataItem>> ConvertEnumWithDescription<T>() where T : struct, IConvertible
{
if (!typeof(T).IsEnum)
{
throw new Exception("Type given T must be an Enum");
}

var enumType = typeof(T).ToString().Split('.').Last();
var itemsList = Enum.GetValues(typeof(T))
.Cast<T>()
.Select(x => new KeyValueDataItem
{
Key = Convert.ToInt32(x),
Value = GetEnumDescription<T>(x.ToString())
})
.ToList();

var res = new KeyValuePair<string, List<KeyValueDataItem>>(
enumType, itemsList);
return res;

}

public static string GetEnumDescription<T>(string value)
{
Type type = typeof(T);
var name = Enum.GetNames(type).Where(f => f.Equals(value, StringComparison.CurrentCultureIgnoreCase)).Select(d => d).FirstOrDefault();

if (name == null)
{
return string.Empty;
}
var field = type.GetField(name);
var customAttribute = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
return customAttribute.Length > 0 ? ((DescriptionAttribute)customAttribute[0]).Description : name;
}

Based on: http://www.extensionmethod.net/csharp/enum/getenumdescription

Get Enum Value By Description


        Rule f;
var type = typeof(Rule);
foreach (var field in type.GetFields())
{
var attribute = Attribute.GetCustomAttribute(field,
typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attribute != null)
{
if (attribute.Description == "description"){
f = (Rule)field.GetValue(null);
break;}
}
else
{
if (field.Name == "description"){
f = (Rule)field.GetValue(null);
break;}
}
}

ref: Get Enum from Description attribute

Convert Enum with [DescriptionAttribute] to Dictionary string,int

According to an answer https://stackoverflow.com/a/2650090/643095,

string description = Enumerations.GetEnumDescription((MyEnum)value);

Where value is int enum value.

You can use something like this:

accountViewModel.Genders = Enum.GetValues(typeof(Account.Genders))
.Cast<Account.Genders>()
.ToDictionary(t => Enumerations.GetEnumDescription((Genders)t), t => (int)t);


Related Topics



Leave a reply



Submit