KeyedByTypeCollection.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / KeyedByTypeCollection.cs / 1 / KeyedByTypeCollection.cs

                            //------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------

namespace System.Collections.Generic 
{
    using System.Xml; 
    using System.ServiceModel; 
    using System.Collections.ObjectModel;
    using System.Runtime.Serialization; 

    public class KeyedByTypeCollection : KeyedCollection
    {
        public KeyedByTypeCollection() 
            : base(null, 4)
        { 
        } 

        public KeyedByTypeCollection(IEnumerable items) 
            : base(null, 4)
        {
            if (items == null)
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("items"); 

            foreach (TItem item in items) 
            { 
                base.Add(item);
            } 
        }

        public T Find()
        { 
            return this.Find(false);
        } 
 
        public T Remove()
        { 
            return this.Find(true);
        }

        T Find(bool remove) 
        {
            for (int i = 0; i < this.Count; i++) 
            { 
                TItem settings = this[i];
                if (settings is T) 
                {
                    if (remove)
                    {
                        Remove(settings); 
                    }
                    return (T)(object)settings; 
                } 
            }
            return default(T); 
        }

        public Collection FindAll()
        { 
            return this.FindAll(false);
        } 
 
        public Collection RemoveAll()
        { 
            return this.FindAll(true);
        }

        Collection FindAll(bool remove) 
        {
            Collection result = new Collection(); 
            foreach (TItem settings in this) 
            {
                if (settings is T) 
                {
                    result.Add((T)(object)settings);
                }
            } 

            if (remove) 
            { 
                foreach (T settings in result)
                { 
                    this.Remove((TItem)(object)settings);
                }
            }
 
            return result;
        } 
 
        protected override Type GetKeyForItem(TItem item)
        { 
            if (item == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("item");
            } 

            return item.GetType(); 
        } 

        protected override void InsertItem(int index, TItem item) 
        {
            if (item == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("item"); 
            }
 
            if (this.Contains(item.GetType())) 
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("item", SR.GetString(SR.DuplicateBehavior1, item.GetType().FullName)); 
            }

            base.InsertItem(index, item);
        } 

        protected override void SetItem(int index, TItem item) 
        { 
            if (item == null)
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("item");
            }

            base.SetItem(index, item); 
        }
    } 
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.


                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK