Exemple #1
0
        ///  <summary>
        ///  Dispatches the specified event to each listener.
        ///  </summary>
        /// <param name="eventType">Event type</param>
        /// <param name="item">Item</param>
        private void DispatchEvent(int eventType, TItem item)
        {
            object[] listenerSet;
            lock (_registeredListener) {
                listenerSet = _registeredListener.ToArray();
            }

            for (int i = -1; ++i != listenerSet.Length;)
            {
                object rawObj = listenerSet[i];
                IEnumerableListener         rawListener = rawObj as IEnumerableListener;
                IEnumerableListener <TItem> genListener = rawObj as IEnumerableListener <TItem>;

                if (eventType == AddEvent)
                {
                    rawListener?.OnItemAdded(this, item);
                    genListener?.OnItemAdded(this, item);
                    continue;
                }

                if (eventType == RemoveEvent)
                {
                    rawListener?.OnItemRemoved(this, item);
                    genListener?.OnItemRemoved(this, item);
                    continue;
                }

                if (eventType == ClearEvent)
                {
                    rawListener?.OnCleared(this);
                    genListener?.OnCleared(this);
                }
            }
        }
        /// <summary>
        /// Returns a newly created array that contains all items of the given sequence <paramref name="sequence"/>.
        /// </summary>
        /// <param name="sequence">Sequence to process</param>
        /// <returns>Array of all sequence items</returns>
        public static object[] ToArray(this IEnumerable sequence)
        {
            // Check if it's already an array
            if (sequence is object[] objectArray)
            {
                return(objectArray);
            }

            // Check if we need just to perform a copy
            if (sequence is ICollection objectCollection)
            {
                objectArray = new object[objectCollection.Count];
                objectCollection.CopyTo(objectArray, 0);
                return(objectArray);
            }

            // Process items
            LinearList <object> linkedList = new LinearList <object>();

            sequence.ForEach(linkedList.Add);
            object[] resultingArray = linkedList.ToArray();
            return(resultingArray);
        }
Exemple #3
0
        /// <summary>
        /// Returns a newly created array that contains all items of the given sequence <paramref name="sequence"/>.
        /// </summary>
        /// <typeparam name="TItem">Type of sequence items</typeparam>
        /// <param name="sequence">Sequence to process</param>
        /// <returns>Array of all sequence items</returns>
        public static TItem[] ToArray <TItem>(this IEnumerable <TItem> sequence)
        {
            // Check if it's already an array
            if (sequence is TItem[] itemArray)
            {
                return(itemArray);
            }

            // Check if we need just to perform a copy
            if (sequence is ICollection <TItem> itemCollection)
            {
                itemArray = new TItem[itemCollection.Count];
                itemCollection.CopyTo(itemArray, 0);
                return(itemArray);
            }

            // Process items
            LinearList <TItem> linkedList = new LinearList <TItem>();

            sequence.ForEach(linkedList.Add);
            TItem[] resultingArray = linkedList.ToArray();
            return(resultingArray);
        }