2010-04-09 82 views
3

我想找出一種方法來利用泛型,所以我可以使屬性值是一個實際的類型初始化(不知道這是否是正確的說法)當我的集合類被創建。泛型和集合...與實現掙扎

我想有語法是這樣的:

var list = new ListItemCollection<Guid>(parameters would go here); 

我有下面的類:

[Serializable] 
public class ListItem 
{ 
    public object Value { get; set; } 
    public string Text { get; set; } 
    public object DataContext { get; set; } 
    public Nullable<bool> Checked { get; set; } 

    public ListItem() 
    { 
     this.Checked = false; 
    } 
} 

我有以下的集合:

[Serializable] 
public class ListItemCollection : List<ListItem> 
{ 
    public ListItem this[object value] 
    { 
     get 
     { 
      foreach (var child in this) 
      { 
       if (child.Value.Equals(value)) 
        return child; 
      } 
      return null; 
     } 
    } 

    public bool Contains(object value) 
    { 
     foreach (var child in this) 
     { 
      if (child.Value.Equals(value)) 
       return true; 
     } 
     return false; 
    } 

    public void Add(object value, string text) 
    { 
     this.Add(value, text, null); 
    } 

    public void Add(object value, string text, object dataContext) 
    { 
     var child = new ListItem(); 
     child.Value = value; 
     child.Text = text; 
     child.DataContext = dataContext; 
     this.Add(child); 
    } 

    public ListItemCollection() 
    { 
    } 

    public ListItemCollection(IEnumerable items, 
     string displayMember, 
     string valueMember, 
     bool showEmptyItem, 
     string emptyItemText, 
     object emptyItemValue) 
    { 
     if (showEmptyItem) 
     { 
      this.Add(emptyItemValue, emptyItemText); 
     } 

     foreach (object item in items) 
     { 
      object text = null; 
      object value = null; 

      text = item.GetType().GetProperty(displayMember).GetValue(item, null); 
      value = item.GetType().GetProperty(valueMember).GetValue(item, null); 

      // Add the item 
      this.Add(value, text.ToString(), item); 
     } 
    } 
} 

回答

6

你可以使ListItem類通用:

[Serializable] 
public class ListItem<T> 
{ 
    public T Value { get; set; } 
    public string Text { get; set; } 
    public object DataContext { get; set; } 
    public Nullable<bool> Checked { get; set; } 

    public ListItem() 
    { 
     this.Checked = false; 
    } 
} 

這使得集合也通用:

[Serializable] 
public class ListItemCollection<T> : List<ListItem<T>> 
{ 
    ... 
} 
2

你只需要做出ListItemListItemCollection通用。

[Serializable] 
public class ListItem<T> 
{ 
    public T Value { get; set; } 
    public string Text { get; set; } 
    public object DataContext { get; set; } 
    public Nullable<bool> Checked { get; set; } 

    public ListItem() 
    { 
     this.Checked = false; 
    } 
} 

[Serializable] 
public class ListItemCollection<T> : List<ListItem<T>> 
{ 
    public ListItem<T> this[T value] 
    { 
     get 
     { 
      foreach (var child in this) 
      { 
       if (object.Equals(child.Value, value)) 
        return child; 
      } 
      return null; 
     } 
    } 

    public bool Contains(T value) 
    { 
     foreach (var child in this) 
     { 
      if (object.Equals(child.Value, value)) 
       return true; 
     } 
     return false; 
    } 

    public void Add(T value, string text) 
    { 
     this.Add(value, text, null); 
    } 

    public void Add(T value, string text, object dataContext) 
    { 
     var child = new ListItem<T>(); 
     child.Value = value; 
     child.Text = text; 
     child.DataContext = dataContext; 
     this.Add(child); 
    } 

    public ListItemCollection() 
    { 
    } 

    public ListItemCollection(IEnumerable items, 
     string displayMember, 
     string valueMember, 
     bool showEmptyItem, 
     string emptyItemText, 
     T emptyItemValue) 
    { 
     if (showEmptyItem) 
     { 
      this.Add(emptyItemValue, emptyItemText); 
     } 

     foreach (object item in items) 
     { 
      object text = null; 
      T value = default(T); 

      text = item.GetType().GetProperty(displayMember).GetValue(item, null); 
      value = (T)item.GetType().GetProperty(valueMember).GetValue(item, null); 

      // Add the item 
      this.Add(value, text.ToString(), item); 
     } 
    } 
} 

(我改變了你的childValue.Value.Equals()調用object.Equals允許空值)。