泛型和集合...在实施中遇到困难

发布于 2024-08-28 06:07:34 字数 2143 浏览 3 评论 0原文

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

我希望语法类似于:

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);
        }
    }
}

I am trying to figure out a way to leverage generics so I can make the property Value be an actual type that initialized (not sure if this is the correct way of saying it) when my collection class is created.

I would like to have the syntax be something like:

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

I have the following class:

[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;
    }
}

I have the following collection:

[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);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮云落日 2024-09-04 06:07:34

您可以使 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>>
{
    ...
}

You could make the ListItem class generic:

[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;
    }
}

Which makes the collection also generic:

[Serializable]
public class ListItemCollection<T> : List<ListItem<T>>
{
    ...
}
吐个泡泡 2024-09-04 06:07:34

您只需将 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 以允许空值。)

You'll just need to make ListItem and ListItemCollection generic.

[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);
        }
    }
}

(I changed your childValue.Value.Equals() calls to object.Equals to allow for null values.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文