class StringBase : CollectionBase
...{
public string this[int index]
...{
get ...{ return ((string)List[index]); }
set ...{ List[index] = value; }
}
public int Add(string value)
...{
return (List.Add(value));
}
public int IndexOf(string value)
...{
return (List.IndexOf(value));
}
public void Insert(int index, string value)
...{
List.Insert(index, value);
}
public void Remove(string value)
...{
List.Remove(value);
}
public bool Contains(string value)
...{
return (List.Contains(value));
}
protected override void OnInsert(int index, object value)
...{
if (value.GetType() != Type.GetType("System.String"))
throw new ArgumentException("value must be of type string.", "value");
base.OnInsert(index, value);
}
protected override void OnRemove(int index, object value)
...{
if (value.GetType() != Type.GetType("System.String"))
throw new ArgumentException("value must be of type string.", "value");
base.OnRemove(index, value);
}
protected override void OnSet(int index, object oldValue, object newValue)
...{
if (newValue.GetType() != Type.GetType("System.String"))
throw new ArgumentException("newValue must be of type string.", "newValue");
base.OnSet(index, oldValue, newValue);
}
protected override void OnValidate(object value)
...{
if (value.GetType() != Type.GetType("System.String"))
throw new ArgumentException("value must be of type string.");
base.OnValidate(value);
}
}
本文介绍了一个自定义的StringBase类,该类继承自CollectionBase,并实现了字符串集合的基本操作,如添加、插入、删除等。此外,为了确保集合中只包含字符串类型的数据,还实现了类型检查以防止插入非字符串类型的值。
1711

被折叠的 条评论
为什么被折叠?



