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.