Reputation: 73
All,
I'm trying to sort out my last bits in a Enum-framework.
My goal: I want to send any enum type and convert it to a List and bind it to a drop down list. i will use an ObjectDataSource as DataSource for the given drop down list. I want to create a composite control that only takes one parameter; enum type. The composite control will sort out databinding and all other bits and bops.
Now, the only problem I have is to convert the generic method to be compatible with the ObjectDataSource.
Here is the code for my current method that i need to use on my ObjectDataSource. So, this method is working fine and returns a list of items for the Enum type WeekDays. However, I need the same functionality, but I need to replace WeekDays with any type of enum.
Code:
public class DropDownData
{
public EnumDataItemList GetList()
{
EnumDataItemList items = new EnumDataItemList();
foreach (int value in Enum.GetValues(WeekDays))
{
EnumDataItem item = new EnumDataItem();
WeekDays d = (WeekDays)value;
//Set display text
if (!string.IsNullOrEmpty(DataHandlers.GetAttributeValue<DisplayTextAttribute, string>(d)))
{
//Translation logic goes here
item.Text = DataHandlers.GetAttributeValue<DisplayTextAttribute, string>(d);
}
else
{
//Translation logic goes here
item.Text = Enum.GetName(typeof(WeekDays), value);
}
item.Value = value; //Actual value
item.ToolTip = DataHandlers.GetAttributeValue<ToolTipAttribute, string>(d);
item.Description = DataHandlers.GetAttributeValue<Lia.Library.Enums.CustomAttributes.DescriptionAttribute, string>(d);
item.HelpText = DataHandlers.GetAttributeValue<HelpTextAttribute, string>(d);
item.ExcludeOnBinding = DataHandlers.GetAttributeValue<ExcludeOnBinding, bool>(d);
if (!item.ExcludeOnBinding)
{
items.Add(item);
}
}
return items;
}
}
public class EnumDataItemList : List<EnumDataItem>
{
}
As far as I know, I can't use a generic method with ObjectDataSOurce, but Generic Class is fine. I just can't get it to work with a generic class and all help is much appreciated. When all is working, I'll be happy to share the complete solution.
I'm using Framework 2.0.
Upvotes: 0
Views: 11003
Reputation: 1876
I seem to have gone at this problem a little differently and came up with something fairly terse that appears to work for me. I cannot claim this as original work since I don't remember if I found it and copied it, or if I put it together from bits and pieces I found, with some original work of my own. I put an extension method on enums that gives me a ToDisplayText function for getting my custom enum attribute of the same name.
this.ddlBlah.DataSource =
Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>()
.ToDictionary(x => x, x => x.ToDisplayText());
this.ddlBlahs.DataValueField = "key";
this.ddlBlah.DataTextField = "value";
this.ddlBlah.DataBind();
public static string ToDisplayText(this Enum Value)
{
try
{
Type type = Value.GetType();
MemberInfo[] memInfo = type.GetMember(Value.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(
typeof(DisplayText),
false);
if (attrs != null && attrs.Length > 0)
return ((DisplayText)attrs[0]).DisplayedText;
}
}
catch (Exception ex)
{
throw new Exception("Your favorite error handling here");
}
return Value.ToString();
// End of ToDisplayText()
}
[System.AttributeUsage(System.AttributeTargets.Field)]
public class DisplayText : System.Attribute
{
public string DisplayedText;
public DisplayText(string displayText)
{
DisplayedText = displayText;
}
// End of DisplayText class definition
}
Upvotes: 0
Reputation: 46997
This should help you. (Will throw Exception if T is not an enum type.)
public static EnumDataItemList GetList<T>() where T : struct
{
EnumDataItemList items = new EnumDataItemList();
foreach (int e in Enum.GetValues(typeof(T)))
{
EnumDataItem item = new EnumDataItem();
item.Text = Enum.GetName(typeof(T), e);
item.Value = e;
}
//Rest of code goes here
}
Usage:
EnumDataItemList days = GetList<WeekDays>();
if you don't want to use a generic method, you can change it to:
public static EnumDataItemList GetList(Type t)
{
EnumDataItemList items = new EnumDataItemList();
foreach (int e in Enum.GetValues(t))
{
EnumDataItem item = new EnumDataItem();
item.Text = Enum.GetName(t, e);
item.Value = e;
}
//Rest of code goes here
}
Upvotes: 3
Reputation: 7449
Alternative to Magnus', but the idea is pretty much exactly the same (just didn't want to throw it out after being beat to it ;-) ) - just iterates over the enum value rather than int. Same usage:
public static class DropDownData
{
// struct, IComparable, IFormattable, IConvertible is as close as we'll
// get to an Enum constraint. We don't actually use the constraint for
// anything except rudimentary compile-time type checking, though, so
// you may leave them out.
public static EnumDataItemList GetList<T>()
where T : struct, IComparable, IFormattable, IConvertible
{
// Just to make the intent explicit. Enum.GetValues will do the
// type check, if this is left out:
if (!typeof(T).IsEnum)
{
throw new ArgumentException("Type must be an enumeration");
}
EnumDataItemList items = new EnumDataItemList();
foreach (Enum e in Enum.GetValues(typeof(T)))
{
EnumDataItem items = new EnumDataItem();
// Note: This assumes the enum's underlying type is
// assignable to Int32 (for example, not a long):
int value = Convert.ToInt32(e);
// The same attribute retrieval code as in the
// WeekDays example, including:
item.Text = e.ToString(); // e is Enum here, no need for GetName
}
}
}
Upvotes: 2