为什么没有 ObservableKeyedCollection在.NET框架中?

发布于 2024-08-23 13:54:50 字数 585 浏览 6 评论 0 原文

.NET Framework 从 3.0 版开始包含 ObservableCollection,但为什么没有 ObservableKeyedCollection

好吧,我可以通过派生 KeyedCollection 并实现 INotifyCollectionChanged 接口,但它不是对 .NET Framework 的一个很好的补充吗?

The .NET Framework contains since version 3.0 the ObservableCollection<T>, but why isn´t there a ObservableKeyedCollection<TKey, TValue>.

Okay i could implement my own collection by deriving from KeyedCollection<TKey,TValue> and implementing the INotifyCollectionChanged interface, but whouldn´t it be a good addition to the .NET Framework.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

欢烬 2024-08-30 13:54:50

没有 ObservableKeyedCollection (或任何其他此类类型,只是其他泛型类型的组合)的原因是因为 ObservableCollection 是通用的,这使得“ObservableKeyedCollection”的实现像这样简单:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

public class DictionaryWatcher : ObservableCollection<KeyValuePair<string, object>>, IDisposable
{
    private NotifyCollectionChangedEventHandler watcher;
    private bool watching = false;

    public DictionaryWatcher()
    {
        watcher = new NotifyCollectionChangedEventHandler( ReportChange );
        CollectionChanged += watcher;
        Watched = true;
    }

    public bool Watched
    {
        get
        {
            return watching;
        }

        set
        {
            if (watching)
            {
                lock (this)
                {
                    CollectionChanged -= watcher;
                    watching = false;
                }
            }
        }
    }

public void Dispose()
{
    Dispose( true );
    GC.SuppressFinalize( this );
}

    public void Initialize()
    {
        this.Add( new KeyValuePair<string, object>( "First", 1 ) );
        this.Add( new KeyValuePair<string, object>( "Second", 2 ) );
        this.Add( new KeyValuePair<string, object>( "Turd", 3 ) );
        KeyValuePair<string, object> badValue = this[2];
        this.Remove( badValue );
    }

protected virtual void Dispose( bool disposing )
{
    if (disposing && Watched)
    {
        Watched = false;
    }
}

    private void ReportChange( object sender, NotifyCollectionChangedEventArgs e )
    {
        Console.WriteLine( "Change made: {0}", e.Action );
    }
}

虽然这肯定不是一个单行程序,但其中大部分都是样板文件。最重要的是,它不会像您一样重新实现 ObservableCollection正在建议;相反,它充分利用了它。

它“不是 .NET Framework 的一个好的补充”的原因是,当已经有一种方法可以做某事时,创建另一种方法来完成某件事是一个坏主意。完成某项特定任务的方法越少,做得不好的方法就越少。 8)

工具已经提供了,现在就看你如何使用它们了。

希望有帮助!

The reason that there is no ObservableKeyedCollection (or any other such type which is merely a combination of other generic types) is because ObservableCollection is generic, and that makes implementation of an "ObservableKeyedCollection" as easy as this:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

public class DictionaryWatcher : ObservableCollection<KeyValuePair<string, object>>, IDisposable
{
    private NotifyCollectionChangedEventHandler watcher;
    private bool watching = false;

    public DictionaryWatcher()
    {
        watcher = new NotifyCollectionChangedEventHandler( ReportChange );
        CollectionChanged += watcher;
        Watched = true;
    }

    public bool Watched
    {
        get
        {
            return watching;
        }

        set
        {
            if (watching)
            {
                lock (this)
                {
                    CollectionChanged -= watcher;
                    watching = false;
                }
            }
        }
    }

public void Dispose()
{
    Dispose( true );
    GC.SuppressFinalize( this );
}

    public void Initialize()
    {
        this.Add( new KeyValuePair<string, object>( "First", 1 ) );
        this.Add( new KeyValuePair<string, object>( "Second", 2 ) );
        this.Add( new KeyValuePair<string, object>( "Turd", 3 ) );
        KeyValuePair<string, object> badValue = this[2];
        this.Remove( badValue );
    }

protected virtual void Dispose( bool disposing )
{
    if (disposing && Watched)
    {
        Watched = false;
    }
}

    private void ReportChange( object sender, NotifyCollectionChangedEventArgs e )
    {
        Console.WriteLine( "Change made: {0}", e.Action );
    }
}

While that is certainly not a one-liner program, most of it is boilerplate. Most importantly, it doesn't re-implement the ObservableCollection as you were suggesting; instead it fully utilizes it.

The reason that it "whouldn't be a good addition to the .NET Framework" is because when there's already one way to do something, creating another way to do it is a bad idea. The fewer ways there are to get some particular task done, the fewer ways there are to do it poorly. 8 )

The tools are provided, it's now all about how you use them.

Hope that helps!

貪欢 2024-08-30 13:54:50

请查看 ObservableKeyedCollection 类实现。这很容易。

Please take a look at the ObservableKeyedCollection class implementation. It's pretty easy.

幽蝶幻影 2024-08-30 13:54:50

我建议您查看 C5。它是一个很棒的通用集合库,提供可观察集合作为其所有集合的标准,包括 AddedInsertedRemoved已删除已清除已更改。此外,C5 系列都拥护“接口编程”理念。所有接口都提供了底层实现的完整功能,这是 System.Collections.Generic 命名空间所缺少的。此外,还有完整的文档。我强烈建议您检查一下。

I'd recommend you take a look at C5. It is a wonderful generic collection library that offers observable collections as standard for all of its collections, including Added, Inserted, Removed, RemovedAt, Cleared, and Changed. Additionally, the C5 collections all espouse the "programming to interface" ideal. All of the interfaces provides expose the full functionality of the underlying implementations—which is lacking in the System.Collections.Generic namespace. Additionally, there is thorough documentation. I highly encourage you to check it out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文