代码之家  ›  专栏  ›  技术社区  ›  mattruma

泛型和集合…在实现方面遇到困难

  •  3
  • mattruma  · 技术社区  · 15 年前

    我正试图找出一种方法来利用泛型,这样我就可以使属性值成为在创建集合类时初始化的实际类型(不确定这是否是正确的表达方式)。

    我希望语法如下:

    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);
            }
        }
    }
    
    2 回复  |  直到 15 年前
        1
  •  6
  •   Darin Dimitrov    15 年前

    你可以做 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
  •  2
  •   Adam Robinson    15 年前

    你只需要 ListItem ListItemCollection 通用。

    [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 允许空值。)