コード例 #1
0
        public virtual void AddAt(int index, TKey key, TValue item)
        {
            DictionaryBeforeEventArgs <TKey, TValue> e = new DictionaryBeforeEventArgs <TKey, TValue>
                                                             (key, item);

            // Raise before events:
            bool bubble = true;

            if (BeforeAdd != null)
            {
                foreach (DictionaryBeforeDelegate <TKey, TValue> function in BeforeAdd.GetInvocationList())
                {
                    e.Bubble = true;
                    function.Invoke(this, e);
                    bubble = bubble && e.Bubble;
                }
            }
            if (!bubble)
            {
                return;
            }

            // Add item:
            // Use value returend by event:
            if (m_bReplaceDuplicateKeys && ContainsKey(TransformKey(e.Key)))   //check if it contains and remove
            {
                Remove(TransformKey(e.Key));
            }

            base.Add(TransformKey(e.Key), e.Value);

            if (index != -1)
            {
                m_col.Insert(index, e.Key);
            }
            else
            {
                m_col.Add(e.Key);
            }

            // Raise after events:
            if (AfterAdd != null)
            {
                AfterAdd.Invoke(this, e);
            }
        }
コード例 #2
0
        public new void Remove(TKey key)
        {
            bool bContains = ContainsKey(TransformKey(key));

            if (m_bThrowErrorOnInvalidRemove && !bContains)
            {
                throw new IndexedDictionaryException("Key does not exist within the Dictionary");
            }
            else if (!bContains)
            {
                return;
            }

            // Raise before events:
            DictionaryBeforeEventArgs <TKey, TValue> e = new DictionaryBeforeEventArgs <TKey, TValue>
                                                             (key, base[TransformKey(key)]);

            // Raise before events:
            bool bubble = true;

            if (BeforeRemove != null)
            {
                foreach (DictionaryBeforeDelegate <TKey, TValue> function in BeforeRemove.GetInvocationList())
                {
                    e.Bubble = true;
                    function.Invoke(this, e);
                    bubble = bubble && e.Bubble;
                }
            }
            if (!bubble)
            {
                return;
            }

            // Remove item:
            // Use value returend by event:
            m_col.Remove(e.Key);
            base.Remove(TransformKey(e.Key));

            // Raise after events:
            if (AfterRemove != null)
            {
                AfterRemove.Invoke(this, e);
            }
        }