Reputation: 77
Good morning. I have the need to create a little helper class, using generics, however my knowledge of generics is very low. So here is what i need. i have defined enums in C#, to have description properties. for example
public enum EnumLineItemErrorCode
{
[Description("None")]
None = 0,
[Description("helpful Desc")]
MissnigA= 1,
[Description("another desc")]
MissingB = 2
}
I also have created helper functions that get he description out of the enums like
public static string GetEnumDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
return attributes.Length > 0 ? attributes[0].Description : value.ToString();
}
Now if i need the desc i can call the method like the following
EnumerationsHelper.GetEnumDescription(EnumLineItemErrorCode.MissnigA);
However when we want to do bindinds to a datasource i currently do the following for each value on the enum.
dropdownList.Add(new ListItem(EnumerationsHelper.GetEnumDescription(EnumLineItemErrorCode.MissnigA), EnumLineItemErrorCode.MissnigA.ToString()));
But this approach is inflexible as the enum grows in size and also because it does not automatically adds a value to the list if i just add it to the enum.
So my question is.
Can u create a helper method that will return me a list of Description, Value, where description is the enum description and the value is the enum internal value. For example on the code that i have will be used as
object t EnumerationsHelper.GetDescriptionAndValuesAslist(EnumLineItemErrorCode);
and object t is structure of with values <"None",None">,<"MissnigA","helpful Desc">,<"MissingB","another desc">
Thank you in advance.
Upvotes: 0
Views: 256
Reputation: 41
I quickly wrote the following helper method for you
public static IDictionary<string, string> GetEnumBindings<T>()
{
if (!typeof(Enum).IsAssignableFrom(typeof(T)))
{
throw new ArgumentException("The provided type is not an enum");
}
var result = new Dictionary<string, string>();
var fieldNames = Enum.GetNames(typeof (T));
foreach (var fieldName in fieldNames)
{
var fieldAttributes = typeof (T).GetField(fieldName)
.GetCustomAttributes(typeof (DescriptionAttribute), false);
var description = fieldAttributes.Any()
? ((DescriptionAttribute) fieldAttributes.First()).Description
: fieldName;
result.Add(fieldName, description);
}
return result;
}
And use case is:
var bindings = GetEnumBindings<EnumLineItemErrorCode>();
var listItems = bindings.Select(b => new ListItem(b.Value, b.Key));
After that just add listItems to your DropDownList. Good luck!
UPD: Since description is not unique, I modified code a little bit to add field name as a key to the dictionary.
Upvotes: 0
Reputation: 73482
This will do the job.
private string[] GetEnumDescriptions<T>()
{
return Enum.GetValues(typeof(T))
.Cast<Enum>()
.Select(GetEnumDescription)
.ToArray();
}
Use case:
var descriptions = GetDescriptions<EnumLineItemErrorCode>();
Note: If T is not of type Enum
Enum.GetValues
method will throw exception.
Upvotes: 1