Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 属性网格中的自定义集合_C#_Collections_Propertygrid - Fatal编程技术网

C# 属性网格中的自定义集合

C# 属性网格中的自定义集合,c#,collections,propertygrid,C#,Collections,Propertygrid,我将本文用作在propertygrid中使用自定义集合的参考: 当我打开collectioneditor并删除所有项目时,然后按OK,如果为null,则会出现异常 我如何解决这个问题? 我正在使用: public T this[int index] { get { if (List.Count == 0) { return default(T); } else { return (T)t

我将本文用作在propertygrid中使用自定义集合的参考:

当我打开collectioneditor并删除所有项目时,然后按OK,如果为null,则会出现异常

我如何解决这个问题?
我正在使用:

public T this[int index]
{
   get
   {
      if (List.Count == 0)
      {
         return default(T);
      }
      else
      {
         return (T)this.List[index];
      }
   }
}
作为一个项目的getter,当然,如果我没有对象,如何重新启动整个集合

这就是全部代码

/// <summary>
/// A generic folder settings collection to use in a property grid.
/// </summary>
/// <typeparam name="T">can be import or export folder settings.</typeparam>
[Serializable]
[TypeConverter(typeof(FolderSettingsCollectionConverter)), Editor(typeof(FolderSettingsCollectionEditor), typeof(UITypeEditor))]
public class FolderSettingsCollection_New<T> : CollectionBase, ICustomTypeDescriptor
{
    private bool m_bRestrictNumberOfItems;
    private int m_bNumberOfItems;
    private Dictionary<string, int> m_UID2Idx = new Dictionary<string, int>();
    private T[] arrTmp;

    /// <summary>
    /// C'tor, can determine the number of objects to hold.
    /// </summary>
    /// <param name="bRestrictNumberOfItems">restrict the number of folders to hold.</param>
    /// <param name="iNumberOfItems">The number of folders to hold.</param>
    public FolderSettingsCollection_New(bool bRestrictNumberOfItems = false , int iNumberOfItems = 1)
    {
        m_bRestrictNumberOfItems = bRestrictNumberOfItems;
        m_bNumberOfItems = iNumberOfItems;
    }

    /// <summary>
    /// Add folder to collection.
    /// </summary>
    /// <param name="t">Folder to add.</param>
    public void Add(T t)
    {
        if (m_bRestrictNumberOfItems)
        {
            if (this.List.Count >= m_bNumberOfItems)
            {
                return;
            }
        }

        int index = this.List.Add(t);

        if (t is WriteDataFolderSettings || t is ReadDataFolderSettings)
        {
            FolderSettingsBase tmp = t as FolderSettingsBase;
            m_UID2Idx.Add(tmp.UID, index);
        }
    }

    /// <summary>
    /// Remove folder to collection.
    /// </summary>
    /// <param name="t">Folder to remove.</param>
    public void Remove(T t)
    {
        this.List.Remove(t);

        if (t is WriteDataFolderSettings || t is ReadDataFolderSettings)
        {
            FolderSettingsBase tmp = t as FolderSettingsBase;
            m_UID2Idx.Remove(tmp.UID);
        }
    }

    /// <summary>
    /// Gets ot sets a folder.
    /// </summary>
    /// <param name="index">The index of the folder in the collection.</param>
    /// <returns>A folder object.</returns>
    public T this[int index]
    {
        get
        {
            //if (List.Count == 0)
            //{
            //   return default(T);
            //}
            //else
            //{
                return (T)this.List[index];
            //}
        }
    }

    /// <summary>
    /// Gets or sets a folder.
    /// </summary>
    /// <param name="sUID">The UID of the folder.</param>
    /// <returns>A folder object.</returns>
    public T this[string sUID]
    {
        get
        {
            if (this.Count == 0 || !m_UID2Idx.ContainsKey(sUID))
            {
                return default(T);
            }
            else
            {
                return (T)this.List[m_UID2Idx[sUID]];
            }
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sUID"></param>
    /// <returns></returns>
    public bool ContainsItemByUID(string sUID)
    {
        return m_UID2Idx.ContainsKey(sUID);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public String GetClassName()
    {
        return TypeDescriptor.GetClassName(this, true);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public AttributeCollection GetAttributes()
    {
        return TypeDescriptor.GetAttributes(this, true);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public String GetComponentName()
    {
        return TypeDescriptor.GetComponentName(this, true);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public TypeConverter GetConverter()
    {
        return TypeDescriptor.GetConverter(this, true);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public EventDescriptor GetDefaultEvent()
    {
        return TypeDescriptor.GetDefaultEvent(this, true);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public PropertyDescriptor GetDefaultProperty()
    {
        return TypeDescriptor.GetDefaultProperty(this, true);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="editorBaseType"></param>
    /// <returns></returns>
    public object GetEditor(Type editorBaseType)
    {
        return TypeDescriptor.GetEditor(this, editorBaseType, true);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="attributes"></param>
    /// <returns></returns>
    public EventDescriptorCollection GetEvents(Attribute[] attributes)
    {
        return TypeDescriptor.GetEvents(this, attributes, true);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public EventDescriptorCollection GetEvents()
    {
        return TypeDescriptor.GetEvents(this, true);
    }


    /// <summary>
    /// 
    /// </summary>
    /// <param name="pd"></param>
    /// <returns></returns>
    public object GetPropertyOwner(PropertyDescriptor pd)
    {
        return this;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="attributes"></param>
    /// <returns></returns>
    public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
    {
        return GetProperties();
    }

    /// <summary>
    /// Called to get the properties of this type.
    /// </summary>
    /// <returns></returns>
    public PropertyDescriptorCollection GetProperties()
    {
        // Create a collection object to hold property descriptors
        PropertyDescriptorCollection pds = new PropertyDescriptorCollection(null);

        // Iterate the list of employees
        for (int i = 0; i < this.List.Count; i++)
        {
            // Create a property descriptor for the employee item and add to the property descriptor collection
            CollectionPropertyDescriptor_New<T> pd = new CollectionPropertyDescriptor_New<T>(this, i);
            pds.Add(pd);
        }
        // return the property descriptor collection
        return pds;
    }

    public T[] ToArray()
    {
        if (arrTmp == null)
        {
            arrTmp = new T[List.Count];
            for (int i = 0; i < List.Count; i++)
            {
                arrTmp[i] = (T)List[i];
            }
        }

        return arrTmp;
    }

}

/// <summary>
/// Enable to display data about a collection in a property grid.
/// </summary>
/// <typeparam name="T">Folder object.</typeparam>
public class CollectionPropertyDescriptor_New<T> : PropertyDescriptor
{
    private FolderSettingsCollection_New<T> collection = null;
    private int index = -1;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="coll"></param>
    /// <param name="idx"></param>
    public CollectionPropertyDescriptor_New(FolderSettingsCollection_New<T> coll, int idx) : base("#" + idx.ToString(), null)
    {
        this.collection = coll;
        this.index = idx;
    }

    /// <summary>
    /// 
    /// </summary>
    public override AttributeCollection Attributes
    {
        get
        {
            return new AttributeCollection(null);
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="component"></param>
    /// <returns></returns>
    public override bool CanResetValue(object component)
    {
        return true;
    }

    /// <summary>
    /// 
    /// </summary>
    public override Type ComponentType
    {
        get
        {
            return this.collection.GetType();
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public override string DisplayName
    {
        get
        {
            if (this.collection[index] != null)
            {
                return this.collection[index].ToString();
            }
            else
            {
                return null;
            }
        }
    }

    public override string Description
    {
        get
        {
            return "";
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="component"></param>
    /// <returns></returns>
    public override object GetValue(object component)
    {
        if (this.collection[index] != null)
        {
            return this.collection[index];
        }
        else
        {
            return null;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public override bool IsReadOnly
    {
        get { return false; }
    }

    public override string Name
    {
        get { return "#" + index.ToString(); }
    }

    /// <summary>
    /// 
    /// </summary>
    public override Type PropertyType
    {
        get { return this.collection[index].GetType(); }
    }

    public override void ResetValue(object component)
    {

    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="component"></param>
    /// <returns></returns>
    public override bool ShouldSerializeValue(object component)
    {
        return true;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="component"></param>
    /// <param name="value"></param>
    public override void SetValue(object component, object value)
    {
        // this.collection[index] = value;
    }
}
//
///要在特性网格中使用的常规文件夹设置集合。
/// 
///可以导入或导出文件夹设置。
[可序列化]
[TypeConverter(typeof(FolderSettingsCollectionConverter))、编辑器(typeof(FolderSettingsCollectionEditor)、typeof(UITypeEditor))]
公共类FolderSettingsCollection_New:CollectionBase,ICustomTypeDescriptor
{
私人布卢姆·布雷斯特罗夫茨物品;
私有整数m_bnofitems;
私有字典m_UID2Idx=新字典();
私人T[]arrTmp;
/// 
///C'tor可以确定要保存的对象的数量。
/// 
///限制要保存的文件夹数。
///要保存的文件夹数。
公共文件夹设置集合\u新建(bool-bRestrictNumberOfItems=false,int-iNumberOfItems=1)
{
m_bRestrictNumberOfItems=bRestrictNumberOfItems;
m_bNumberOfItems=iNumberOfItems;
}
/// 
///将文件夹添加到集合。
/// 
///要添加的文件夹。
公共无效添加(T)
{
if(m_bRestrictNumberOfItems)
{
如果(this.List.Count>=m_bNumberOfItems)
{
返回;
}
}
int index=this.List.Add(t);
如果(t为WriteDataFolderSettings | | t为ReadDataFolderSettings)
{
FolderSettingsBase tmp=t作为FolderSettingsBase;
m_UID2Idx.Add(tmp.UID,索引);
}
}
/// 
///删除要收集的文件夹。
/// 
///要删除的文件夹。
公共空间移除(T)
{
此.List.Remove(t);
如果(t为WriteDataFolderSettings | | t为ReadDataFolderSettings)
{
FolderSettingsBase tmp=t作为FolderSettingsBase;
m_UID2Idx.Remove(tmp.UID);
}
}
/// 
///获取并设置一个文件夹。
/// 
///集合中文件夹的索引。
///文件夹对象。
公共T此[int索引]
{
得到
{
//如果(List.Count==0)
//{
//返回默认值(T);
//}
//否则
//{
返回(T)此.List[索引];
//}
}
}
/// 
///获取或设置文件夹。
/// 
///文件夹的UID。
///文件夹对象。
公共T此[字符串sUID]
{
得到
{
if(this.Count==0 | |!m_UID2Idx.ContainsKey(sUID))
{
返回默认值(T);
}
其他的
{
返回(T)此.List[m_UID2Idx[sUID]];
}
}
}
/// 
/// 
/// 
/// 
/// 
公共bool containsSiteMBYUID(字符串sUID)
{
返回m_UID2Idx.ContainsKey(sUID);
}
/// 
/// 
/// 
/// 
公共字符串GetClassName()
{
返回TypeDescriptor.GetClassName(此为true);
}
/// 
/// 
/// 
/// 
公共属性集合GetAttributes()
{
返回TypeDescriptor.GetAttributes(this,true);
}
/// 
/// 
/// 
/// 
公共字符串GetComponentName()
{
返回TypeDescriptor.GetComponentName(此为true);
}
/// 
/// 
/// 
/// 
公共类型转换器GetConverter()
{
返回TypeDescriptor.GetConverter(此为true);
}
/// 
/// 
/// 
/// 
公共事件描述符GetDefaultEvent()
{
返回TypeDescriptor.GetDefaultEvent(this,true);
}
/// 
/// 
/// 
/// 
公共属性描述程序GetDefaultProperty()
{
返回TypeDescriptor.GetDefaultProperty(this,true);
}
/// 
/// 
/// 
/// 
/// 
公共对象GetEditor(类型editorBaseType)
{
返回TypeDescriptor.GetEditor(this,editorBaseType,true);
}
/// 
/// 
/// 
/// 
/// 
公共事件描述符集合GetEvents(属性[]属性)
{
返回TypeDescriptor.GetEvents(this,attributes,true);
}
/// 
/// 
/// 
/// 
公共事件描述符集合GetEvents()
{
返回TypeDescriptor.GetEvents(this,true);
}
/// 
/// 
/// 
/// 
/// 
公共对象GetPropertyOwner(PropertyDescriptor pd)
{
归还这个;
}
/// 
/// 
/// 
/// 
/// 
公共属性DescriptorCollection GetProperties(属性[]属性)
{
返回GetProperties();
}
/// 
///调用以获取此类型的属性。
/// 
/// 
公共属性DescriptorCollection GetProperties()
{
//创建集合对象以保存属性描述符
PropertyDescriptorCollection pds=新的PropertyDescriptorCollection(null);
//迭代员工列表
for(int i=0;ipublic T this[int index]
{
   get
   {
      return (T)this.List[index];
   }
}