如何在.NET 2.0中创建自定义集合 [英] How to create a custom collection in .NET 2.0
问题描述
public class MyCollection:System.Collectio.CollectionBase
{
MyCollection(){}
public void Add(MyClass item)
{
this.List.Add(item) ;
}
}
class MyClass
{
public string name;
}
让我问几个问题:
- 这个方法是否正确和新的,因为我在.NET 3.5框架上工作。
- 我想从我的网页上公开这个集合服务(WCF),我该怎么做?
- 我必须实现GetEnumerator吗?
- 是否绑定到DataGridView。 >
从列表中导出
IEnumerable< T>
构造函数和扩展方法的可用性。除了等于
, GetHashCode
和 ToString
。 (我想你可以从 List< T>
派生,如果你的目标是为列表实现Java的toString()功能。)
如果要创建自己的强类型的集合类,并且可以在添加/删除项目时自定义集合行为,则要从新的(到.NET 2.0)类型派生 System.Collections.ObjectModel.Collection< T>
,它保护了虚拟方法,包括 InsertItem
和 RemoveItem
,您可以覆盖这些时间执行操作。请务必阅读文档 - 这是一个非常简单的类,但您必须意识到公共/非虚拟和受保护/虚拟方法之间的区别。 :)
public class MyCollection:Collection< int>
{
public MyCollection()
{
}
public MyCollection(IList< int> list)
:base(list)
{
}
protected override void ClearItems()
{
// TODO:如果需要,验证此处
bool canClearItems = ... ;
如果(!canClearItems)
抛出新的InvalidOperationException(_____时不能清除该集合);
base.ClearItems();
}
protected override void RemoveItem(int index)
{
// TODO:如果需要,验证此处
bool canRemoveItem = ...;
if(!canRemoveItem)
throw new InvalidOperationException(_____时不能删除该项。
base.RemoveItem(index);
}
}
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屋!