0

我开始了一个新项目,我遇到了这个线程: CollectionBase 类仍然支持吗?

因此,我将我的collectionbase课程转换为通用ICollection课程。我的问题是我使用的propertygrid视图collectionbase 适用于我的新课程,但不适用于我的新课程!

Add按钮已禁用:(

[Editor(typeof(System.ComponentModel.Design.CollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
public class SomeCollection<T> : ICollection<T> where T : SomeClass
{
    List<T> SomeList;

    private bool _IsReadOnly = false;
    public bool IsReadOnly
    {
        get
        {
            return _IsReadOnly;
        }
    }

    public SomeCollection() { SomeList = new List<T>(); }

    public T this[int index]
    {
        get
        {
            return (T)SomeList[index];

        }
        set
        {
            SomeList[index] = value;
        }
    }

    public void Add(T b)
    {
        if (!exists(b.Name))
        {
            SomeList.Add(b);
        }
        else
        {
            throw new ArgumentException("bla");
        }
    }

    public int Count
    {
        get
        {
            return SomeList.Count;
        }
    }

    public bool Remove(T b)
    {
        bool Removed = false;
        if (SomeList.Contains(b))
        {
            SomeList.Remove(b);
            Removed = true;
        }
        return Removed;
    }

    public int IndexOf(T b)
    {
        return SomeList.IndexOf(b);
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        for (int i = 0; i < SomeList.Count; i++)
        {

            array[i] = (T)SomeList[i];
        }
    }

    public void AddRange(SomeCollection<T> Collection)
    {
        for (int i = 0; i < Collection.Count(); i++)
        {
            SomeList.Add(Collection[i]);
        }
    }

    public void AddRange(T[] Collection)
    {
        SomeList.AddRange(Collection);
    }

    public bool Contains(T b)
    {
        return SomeList.Contains(b);
    }

    public void Insert(int index, T b)
    {
        SomeList.Insert(index, b);
    }

    public override string ToString()
    {
        if (SomeList.Count() > 0)
        {
            return string.Format("SomeClass ({0})", new object[] { SomeList.Count() });
        }
        else
        {
            return string.Empty;
        }
    }

    public bool exists(string name)
    {
        bool _found = false;
        if (name != null)
        {
            for (int i = 0; i < SomeList.Count; i++)
            {
                if (((SomeClass)SomeList[i]).Address.Equals(name, StringComparison.OrdinalIgnoreCase))
                {
                    _found = true;
                    break;
                }
            }
        }
        return _found;

    }

    public void Clear()
    {
        SomeList.Clear();
    }

    public IEnumerator<T> GetEnumerator()
    {
        return new SomeClassEnumerator<T>(this);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return new SomeClassEnumerator<T>(this);
    }
}
4

1 回答 1

0

IIRC,您需要实施IList(不是通用版本)。

于 2012-07-17T15:05:58.910 回答