如何在.NET 2.0创建自定义集合 [英] How to create a custom collection in .NET 2.0
问题描述
您好我想创建我的自定义集合,我从下面CollectionBase的类派生我的自定义集合类:
公开类MyCollection的:System.Collectio.CollectionBase
{
MyCollection的(){}
公共无效添加(MyClass的项目)
{
this.List.Add(项目) ;
}
}
类MyClass的
{
公共字符串名称;
}
让我来问几个问题:
- 不管这种做法是正确的,新的,因为我在.NET 3.5框架的工作。
- 我想从我的揭露网络这个集合服务(WCF)我。如何能做到这一点?
- 请我一定要实现的GetEnumerator?
- 这是否将绑定到DataGridView的。
从列表与LT推导; T>
是有些无意义,尤其的现在,它有的IEnumerable< T>
构造函数和扩展方法的有效性。它可以除重载equals
,的GetHashCode
和的ToString 无虚方法code>。 (我想你可以从
列表与LT获得; T>
如果你的目标是实现对列表的Java的toString()功能)
如果你想创建自己的强类型的集合类,并可能自定义收集行为将项目添加/删除,要从新来推导(到.NET 2.0)输入的 System.Collections.ObjectModel.Collection< T>
,这保护了虚拟方法,包括 InsertItem
和的removeItem
,您可以覆盖执行在这些时间行动。请务必阅读文档 - 这是一个的很容易的类派生自,但你必须实现公共/非虚拟和保护/虚拟方法之间的差异。 :)
公共类MyCollection的:收集< INT>
{
公共MyCollection的()
{
}
公共MyCollection的(IList的< INT>清单)
:基地(名单)
{
}
保护覆盖无效ClearItems()
{
// TODO:如果验证这里需要
布尔canClearItems = ... ; (!canClearItems)
如果
抛出新的InvalidOperationException异常(以下简称集合不能被清除,而_____。);
base.ClearItems();
}
保护覆盖无效的removeItem(INT指数)
{
// TODO:这里验证如有必要,
布尔canRemoveItem = ...;
如果(!canRemoveItem)
抛出新的InvalidOperationException异常(以下简称项目不能被删除的同时_____。);
base.RemoveItem(指数);
}
}
Hi I want to create my custom collection, I am deriving my custom collection class from CollectionBase class as below:
public class MyCollection : System.Collectio.CollectionBase
{
MyCollection(){}
public void Add(MyClass item)
{
this.List.Add(item);
}
}
class MyClass
{
public string name;
}
Let me ask a few questions:
- Whether this approach is correct and new, as I working on .NET 3.5 framework.
- I want to expose this collection from my web service ( WCF) .How can I do that?
- Do I have to implement GetEnumerator?
- Whether this will Bind to DataGridView.
Deriving from List<T>
is somewhat pointless, especially now that it has the IEnumerable<T>
constructor and the availability of extension methods. It has no virtual methods that you can override except Equals
, GetHashCode
, and ToString
. (I suppose you could derive from List<T>
if your goal was to implement Java's toString() functionality for lists.)
If you want to create your own strongly-typed collection class and possibly customize the collection behavior when items are add/removed, you want to derive from the new (to .NET 2.0) type System.Collections.ObjectModel.Collection<T>
, which has protected virtual methods including InsertItem
and RemoveItem
that you can override to perform actions at those times. Be sure to read the documentation - this is a very easy class to derive from but you have to realize the difference between the public/non-virtual and protected/virtual methods. :)
public class MyCollection : Collection<int>
{
public MyCollection()
{
}
public MyCollection(IList<int> list)
: base(list)
{
}
protected override void ClearItems()
{
// TODO: validate here if necessary
bool canClearItems = ...;
if (!canClearItems)
throw new InvalidOperationException("The collection cannot be cleared while _____.");
base.ClearItems();
}
protected override void RemoveItem(int index)
{
// TODO: validate here if necessary
bool canRemoveItem = ...;
if (!canRemoveItem)
throw new InvalidOperationException("The item cannot be removed while _____.");
base.RemoveItem(index);
}
}
这篇关于如何在.NET 2.0创建自定义集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!