如何检查是否在 C# 中添加、更新或删除了 SortedDictionary

How to check if a SortedDictionary is being added, updated or deleted in C#

我有一个 SortedDictionary 的活动,人们可以购买门票。事件代码对于作为密钥存储的票证是唯一的。

protected SortedDictionary<int, Event> Events = new SortedDictionary<int, Event>();

一个这样的例子:

Events.Add(123, new Event(...));

我实现了一个记录器来跟踪事件。看起来像这样:

class EventLogger
{
    protected SortedDictionary<int, EventLogType[]> Events = new SortedDictionary<int, EventLogType[]>();

    public EventLogger()
    {

    }

    public void Log(int eventCode, EventLogType type)
    {
        KeyValuePair<int, EventLogType[]> e;

        if (Events.ContainsKey(eventCode))
        {
            (e = Events.FirstOrDefault(x => x.Key.Equals(eventCode)))
                .Value[e.Value.Count()] = type;
        }
        else Events.Add(eventCode, new EventLogType[] { type });
    }
}

enum EventLogType
{
    PURCHASE,
    CREATE,
    UPDATE,
    DELETE
}

例如,当我创建一个事件时,我现在必须调用这个 Log 方法:

(new EventLogger()).Log(123, EventLogType.CREATE); // EventLogger instance is stored static

每当我想要更新、删除或创建时,这都会成为一种严重的痛苦。我看着 SO 关于改变 set 对数组的工作方式并尝试了这个:

protected SortedDictionary<int, Event> Events { get; set {
    this.save();
} } = new SortedDictionary<int, Event>();

但这给我带来了 IDE 错误:

Only auto-implemented properties can have initializers.

如何在数组中存储或更新任何内容之前访问 set 调用。如何检查正在执行的操作?因此,与其单独调用日志,不如在 .Add 或更新或删除其值时完成?

我当前的代码如下所示:

Events.Add(123, new Event(...));
Logger.Log(123, EventLogType.CREATE);

当我调用 .Add

时,我希望它记录创建的位置

你为什么不使用继承? 创建一个自己的 "MySortedDictionary" ,它继承自 SortedDictionary 并将所需的事件添加到您的 "MySortedDictionary".

作为示例,您可以尝试以下操作,只是把它砍掉了我很懒,所以 class 事件结构不是最佳的。

using System;
using System.Collections.Generic;

namespace SortedDictionrayTest
{

    class AddingEventArgs<TKey, TValue> : EventArgs
    {
        public TKey Key
        {
            get;
        }

        public TValue Value
        {
            get;
        }

        public AddingEventArgs(TKey key, TValue value)
        {
            this.Key = key;
            this.Value = value;
        }
    }

    class AddedEventArgs<TKey, TValue> : AddingEventArgs<TKey, TValue>
    {
        public AddedEventArgs(TKey key, TValue value) : base(key, value)
        {
        }
    }

    class MySortedDictionay<TKey, TValue> : SortedDictionary<TKey, TValue>
    {
        public event EventHandler<AddingEventArgs<TKey, TValue>> Adding;
        public event EventHandler<AddedEventArgs<TKey, TValue>> Added;

        public new void Add(TKey key, TValue value)
        {
            this.OnAdding(new AddingEventArgs<TKey, TValue>(key, value));
            base.Add(key, value);
            this.OnAdded(new AddedEventArgs<TKey, TValue>(key, value));
        }

        protected virtual void OnAdding(AddingEventArgs<TKey, TValue> args)
        {
            if (this.Adding != null)
            {
                Adding(this, args);
            }
        }

        protected virtual void OnAdded(AddedEventArgs<TKey,TValue> args)
        {
            if (this.Added != null)
            {
                Added(this, args);
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var sortedDict = new MySortedDictionay<int, string>();

            sortedDict.Adding += SortedDict_Adding;
            sortedDict.Added += SortedDict_Added;

            sortedDict.Add(2, "World");
            sortedDict.Add(1, "Hello");

            Console.WriteLine("---");
            foreach (KeyValuePair<int, string> keyValuePair in sortedDict)
            {
                Console.WriteLine($"key: [{keyValuePair.Key}] value: [{keyValuePair.Value}]");
            }
        }

        private static void SortedDict_Adding(object sender, AddingEventArgs<int, string> e)
        {
            Console.WriteLine($"before adding key: [{e.Key}] value: [{e.Value}]");
        }

        private static void SortedDict_Added(object sender, AddedEventArgs<int, string> e)
        {
            Console.WriteLine($"after adding key: [{e.Key}] value: [{e.Value}]");
        }

    }
}