2012-01-29 97 views
0

所有,通用方法,並把任何枚舉作爲參數

我試圖理清一個Enum的框架,我的最後一位。

我的目標:我想發送任何枚舉類型並將其轉換爲列表並將其綁定到下拉列表。我將使用ObjectDataSource作爲DataSource的給定下拉列表。我想創建一個只有一個參數的複合控件;枚舉類型。該複合控件將整理數據綁定和所有其他位和bops。

現在,我唯一的問題是將泛型方法轉換爲與ObjectDataSource兼容。

這是我需要在我的ObjectDataSource上使用的當前方法的代碼。因此,此方法工作正常,並返回Enum類型WeekDays的項目列表。不過,我需要相同的功能,但我需要用任何類型的枚舉來替換WeekDays。

代碼:

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> 
{ 

} 

據我知道,我不能用一個通用的方法與ObjectDataSource控件,但是泛型類是罰款。我不能讓它與泛型類一起工作,所有幫助都非常感謝。當所有人都在工作時,我很樂意分享完整的解決方案。

我正在使用Framework 2.0。

回答

3

這應該對你有幫助。 (會拋出異常,如果T不是枚舉類型。)

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 
} 

用法:

EnumDataItemList days = GetList<WeekDays>(); 

,如果你不希望使用一個通用的方法,你可以把它改成:

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 
} 
2

Magnus'的替代方案,但這個想法幾乎完全相同(只是不想在被擊敗之後拋棄它;-)) - 只是遍歷枚舉值而不是int。相同的使用:

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 
     } 
    } 
} 
0

我似乎在這個問題有點不同也水漲船高,並與一些相當簡潔,似乎對我的工作上來。我不能把它當作原創作品,因爲我不記得我是否發現它並複製它,或者如果我將它從我發現的零星碎片中拼湊出來,並附上一些我自己的原創作品。我在枚舉上放了一個擴展方法,它給了我一個ToDisplayText函數來獲取我的自定義枚舉屬性的同名。

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 
}