如何避免实现所有的IList成员,而且还有它实现了一个集合的基接口? [英] How can I avoid implementing all IList members, and still have a base interface which implements a collection?

查看:115
本文介绍了如何避免实现所有的IList成员,而且还有它实现了一个集合的基接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以实现一个简单的解决方案,我正在寻找一个更好的方法,而不必创建一个复杂的实现类,欣赏一些建议。

How can I implement an easier solution, I'm looking for a better approach instead having to create a complex implementation class, appreciate some advice.

目前我做以下内容:

public interface IAddressCollection : IList<IAddress>
{
      new void Add(IAddress address);    
}



于是我不得不实施的一切东西,如下面的代码。我想避免这样做,我正在寻找一个简单的方法

so then I've got to implement everything something like the below the code. I want to avoid doing this, I'm looking for a simple way.

public class AddressCollection : IAddressCollection
{
    private List<IAddress> _data = new List<IAddress>();

    new public void Add(IAddress applicationAddress)
    {
        // some logic here... e.g.
        if (!this.Any(x => x.AddressType != applicationAddress.AddressType))
        {
            _data.Add(applicationAddress);
        }
    }
    // Plus all the members of IList

修改*的 * 的****

EDIT******

我如何避免实现所有的IList成员,而且还有一个底座接口,实现了一个集?

How can I avoid implementing all IList members, and still have a base interface which implements a collection?

此外ü如何获得5票出了什么问题,是什么呢?严重的是,如果他给了我一个像样的答案,我想给他/她一票,但不是问题的问题。

推荐答案

最常用的方法是从收藏<得出; T>

public class AddressCollection : Collection<IAddress>, IAddressCollection
{
    public AddressCollection() : base(new List<IAddress>())
    {
    }
}

收藏< T> 工具的IList< T>

更新回应评论

如果要实现自定义逻辑添加或删除项目时,最好重写的受保护的虚拟方法的一个或多个 InsertItem SetItem 的removeItem ClearItems ,而比实施新添加方法。

If you want to implement custom logic when adding or removing items, it's better to override one or more of the protected virtual methods InsertItem, SetItem, RemoveItem and ClearItems, rather than implementing a new Add method.

InsertItem 被称为每当公众添加插入方法被调用的元素添加到列表中。同样 SetItem 当一个元素被替换被称为( MyCollection的[指数] =为newValue ),的removeItem 被称为当公众删除方法被调用和 ClearItems 当公众清除方法被调用。

InsertItem is called whenever the public Add or Insert method is called to add an element to the list. Similarly SetItem is called when an element is replaced (myCollection[index] = newValue), RemoveItem is called when the public Remove method is called and ClearItems when the public Clear method is called.

这样做,你将解决弗洛里安Greinacher在意见中提出的关注 - 即使列表多态作为您的自定义逻辑就一定会执行的IList< IAddress> 收藏< IAddress>

By doing this you will address the concern raised by Florian Greinacher in the comments - your custom logic will always be executed even if the list is used polymorphically as an IList<IAddress> or Collection<IAddress>

例如,以配合您的样品,你可以忽略插入/设置不匹配的代码类似如下(未经测试)谓词项目:

For example, to match your sample, you could ignore inserting / setting items that don't match a predicate with code something like the following (untested):

protected override void InsertItem(int index, IAddress newItem)
{
    if (!this.Any(x => x.AddressType != newItem.AddressType)
    {
        base.InsertItem(index, newItem);
    }
    else
    {
        // ... item does not match, do whatever is appropriate e.g. throw an exception
    }
}

protected override void SetItem(int index, IAddress newItem)
{
    ... etc ...
}

这篇关于如何避免实现所有的IList成员,而且还有它实现了一个集合的基接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆