PropertyDescriptorCollection.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / ndp / fx / src / CompMod / System / ComponentModel / PropertyDescriptorCollection.cs / 1 / PropertyDescriptorCollection.cs

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

using System.Diagnostics.CodeAnalysis; 
 
[assembly: SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.Add(System.Object,System.Object):System.Void")]
[assembly: SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.set_Item(System.Int32,System.Object):System.Void")] 
[assembly: SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.set_Item(System.Object,System.Object):System.Void")]

/*
 This class has the HostProtectionAttribute. The purpose of this attribute is to enforce host-specific programming model guidelines, not security behavior. 
 Suppress FxCop message - BUT REVISIT IF ADDING NEW SECURITY ATTRIBUTES.
*/ 
[assembly: SuppressMessage("Microsoft.Security", "CA2112:SecuredTypesShouldNotExposeFields", Scope="type", Target="System.ComponentModel.PropertyDescriptorCollection")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.get_IsFixedSize():System.Boolean")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IEnumerable.GetEnumerator():System.Collections.IEnumerator")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.get_IsFixedSize():System.Boolean")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.ICollection.get_SyncRoot():System.Object")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.Remove(System.Object):System.Void")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.ICollection.get_IsSynchronized():System.Boolean")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.get_IsReadOnly():System.Boolean")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.get_Keys():System.Collections.ICollection")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.get_IsReadOnly():System.Boolean")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.Clear():System.Void")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.IndexOf(System.Object):System.Int32")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.Contains(System.Object):System.Boolean")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.RemoveAt(System.Int32):System.Void")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.GetEnumerator():System.Collections.IEnumerator")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.Add(System.Object,System.Object):System.Void")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.Clear():System.Void")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.get_Values():System.Collections.ICollection")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.Contains(System.Object):System.Boolean")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.GetEnumerator():System.Collections.IDictionaryEnumerator")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.Remove(System.Object):System.Void")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.get_Item(System.Int32):System.Object")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.set_Item(System.Int32,System.Object):System.Void")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.Add(System.Object):System.Int32")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.CopyTo(System.Array,System.Int32):System.Void")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.get_Item(System.Object):System.Object")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.set_Item(System.Object,System.Object):System.Void")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.Insert(System.Int32,System.Object):System.Void")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.ICollection.get_Count():System.Int32")]
[assembly: SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection..ctor(System.ComponentModel.PropertyDescriptor[])")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.get_Count():System.Int32")]
[assembly: SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.get_Item(System.String):System.ComponentModel.PropertyDescriptor")]
[assembly: SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection..ctor(System.ComponentModel.PropertyDescriptor[],System.Boolean)")]
[assembly: SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.GetEnumerator():System.Collections.IEnumerator")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.Find(System.String,System.Boolean):System.ComponentModel.PropertyDescriptor")]
[assembly: SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.Sort(System.String[]):System.ComponentModel.PropertyDescriptorCollection")] 
 
namespace System.ComponentModel {
    using Microsoft.Win32; 
    using System.Collections;
    using System.Collections.Specialized;
    using System.Diagnostics;
    using System.Globalization; 
    using System.Runtime.InteropServices;
    using System.Security.Permissions; 
 
    /// 
    ///     
    ///       Represents a collection of properties.
    ///    
    /// 
    [System.Security.Permissions.HostProtection(Synchronization = true)] 
    public class PropertyDescriptorCollection : ICollection, IList, IDictionary {
 
 
        /// 
        /// An empty PropertyDescriptorCollection that can used instead of creating a new one with no items. 
        /// 
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2112:SecuredTypesShouldNotExposeFields")] // ReadOnly fields - already shipped.
        public static readonly PropertyDescriptorCollection Empty = new PropertyDescriptorCollection(null, true);
 
        private IDictionary          cachedFoundProperties;
        private bool                 cachedIgnoreCase; 
        private PropertyDescriptor[] properties; 
        private int                  propCount = 0;
        private string[]             namedSort; 
        private IComparer            comparer;
        private bool                 propsOwned = true;
        private bool                 needSort = false;
        private bool                 readOnly = false; 

        ///  
        ///     
        ///       Initializes a new instance of the 
        ///       class. 
        ///    
        /// 
        public PropertyDescriptorCollection(PropertyDescriptor[] properties) {
            this.properties = properties; 
            if (properties == null) {
                this.properties = new PropertyDescriptor[0]; 
                this.propCount = 0; 
            }
            else { 
                this.propCount = properties.Length;
            }
            this.propsOwned = true;
        } 

        ///  
        ///     Initializes a new instance of a property descriptor collection, and allows you to mark the 
        ///     collection as read-only so it cannot be modified.
        ///  
        public PropertyDescriptorCollection(PropertyDescriptor[] properties, bool readOnly) : this(properties) {
            this.readOnly = readOnly;
        }
 
        private PropertyDescriptorCollection(PropertyDescriptor[] properties, int propCount, string[] namedSort, IComparer comparer) {
            this.propsOwned = false; 
            if (namedSort != null) { 
               this.namedSort = (string[])namedSort.Clone();
            } 
            this.comparer = comparer;
            this.properties = properties;
            this.propCount = propCount;
            this.needSort = true; 
        }
 
        ///  
        ///    
        ///       Gets the number 
        ///       of property descriptors in the
        ///       collection.
        ///    
        ///  
        public int Count {
            get { 
                return propCount; 
            }
        } 

        /// 
        ///    Gets the property with the specified index
        ///       number. 
        /// 
        public virtual PropertyDescriptor this[int index] { 
            get { 
                if (index >= propCount) {
                    throw new IndexOutOfRangeException(); 
                }
                EnsurePropsOwned();
                return properties[index];
            } 
        }
 
        ///  
        ///    Gets the property with the specified name.
        ///  
        public virtual PropertyDescriptor this[string name] {
            get {
                return Find(name, false);
            } 
        }
 
        ///  
        ///    [To be supplied.]
        ///  
        public int Add(PropertyDescriptor value) {
            if (readOnly) {
                throw new NotSupportedException();
            } 

            EnsureSize(propCount + 1); 
            properties[propCount++] = value; 
            return propCount - 1;
        } 

        /// 
        ///    [To be supplied.]
        ///  
        public void Clear() {
            if (readOnly) { 
                throw new NotSupportedException(); 
            }
 
            propCount = 0;
            cachedFoundProperties = null;
        }
 
        /// 
        ///    [To be supplied.] 
        ///  
        public bool Contains(PropertyDescriptor value) {
            return IndexOf(value) >= 0; 
        }

        /// 
        ///    [To be supplied.] 
        /// 
        public void CopyTo(Array array, int index) { 
            EnsurePropsOwned(); 
            Array.Copy(properties, 0, array, index, Count);
        } 

        private void EnsurePropsOwned() {
            if (!propsOwned) {
               propsOwned = true; 
               if (properties != null) {
                  PropertyDescriptor[] newProps = new PropertyDescriptor[Count]; 
                  Array.Copy(properties, 0, newProps, 0, Count); 
                  this.properties = newProps;
               } 
            }

            if (needSort) {
               needSort = false; 
               InternalSort(this.namedSort);
            } 
        } 

        private void EnsureSize(int sizeNeeded) { 

            if (sizeNeeded <= properties.Length) {
               return;
            } 

            if (properties == null || properties.Length == 0) { 
                propCount = 0; 
                properties = new PropertyDescriptor[sizeNeeded];
                return; 
            }

            EnsurePropsOwned();
 
            int newSize = Math.Max(sizeNeeded, properties.Length * 2);
            PropertyDescriptor[] newProps = new PropertyDescriptor[newSize]; 
            Array.Copy(properties, 0, newProps, 0, propCount); 
            properties = newProps;
        } 

        /// 
        ///    Gets the description of the property with the specified
        ///       name. 
        /// 
        public virtual PropertyDescriptor Find(string name, bool ignoreCase) { 
            lock(this) { 
                PropertyDescriptor p = null;
 
                if (cachedFoundProperties == null || cachedIgnoreCase != ignoreCase) {
                    cachedIgnoreCase = ignoreCase;
                    cachedFoundProperties = new HybridDictionary(ignoreCase);
                } 

                // first try to find it in the cache 
                // 
                object cached = cachedFoundProperties[name];
 
                if (cached != null) {
                    return (PropertyDescriptor) cached;
                }
 
                // Now start walking from where we last left off, filling
                // the cache as we go. 
                // 
                for(int i = 0; i < propCount; i++) {
 
                    if (ignoreCase) {
                        if (String.Equals(properties[i].Name, name, StringComparison.OrdinalIgnoreCase)) {
                            cachedFoundProperties[name] = properties[i];
                            p = properties[i]; 
                            break;
                        } 
                    } 
                    else {
                        if (properties[i].Name.Equals(name)) { 
                            cachedFoundProperties[name] = properties[i];
                            p = properties[i];
                            break;
                        } 
                    }
                } 
 
                return p;
            } 
        }

        /// 
        ///    [To be supplied.] 
        /// 
        public int IndexOf(PropertyDescriptor value) { 
            return Array.IndexOf(properties, value, 0, propCount); 
        }
 
        /// 
        ///    [To be supplied.]
        /// 
        public void Insert(int index, PropertyDescriptor value) { 
            if (readOnly) {
                throw new NotSupportedException(); 
            } 

            EnsureSize(propCount + 1); 
            if (index < propCount) {
                Array.Copy(properties, index, properties, index + 1, propCount - index);
            }
            properties[index] = value; 
            propCount++;
        } 
 
        /// 
        ///    [To be supplied.] 
        /// 
        public void Remove(PropertyDescriptor value) {
            if (readOnly) {
                throw new NotSupportedException(); 
            }
 
            int index = IndexOf(value); 

            if (index != -1) { 
                RemoveAt(index);
            }
        }
 
        /// 
        ///    [To be supplied.] 
        ///  
        public void RemoveAt(int index) {
            if (readOnly) { 
                throw new NotSupportedException();
            }

            if (index < propCount - 1) { 
                Array.Copy(properties, index + 1, properties, index, propCount - index - 1);
            } 
            properties[propCount - 1] = null; 
            propCount--;
        } 

        /// 
        ///    
        ///       Sorts the members of this PropertyDescriptorCollection, using the default sort for this collection, 
        ///       which is usually alphabetical.
        ///     
        ///  
        public virtual PropertyDescriptorCollection Sort() {
            return new PropertyDescriptorCollection(this.properties, this.propCount, this.namedSort, this.comparer); 
        }


        ///  
        ///    
        ///       Sorts the members of this PropertyDescriptorCollection.  Any specified NamedSort arguments will 
        ///       be applied first, followed by sort using the specified IComparer. 
        ///    
        ///  
        public virtual PropertyDescriptorCollection Sort(string[] names) {
            return new PropertyDescriptorCollection(this.properties, this.propCount, names, this.comparer);
        }
 
        /// 
        ///     
        ///       Sorts the members of this PropertyDescriptorCollection.  Any specified NamedSort arguments will 
        ///       be applied first, followed by sort using the specified IComparer.
        ///     
        /// 
        public virtual PropertyDescriptorCollection Sort(string[] names, IComparer comparer) {
            return new PropertyDescriptorCollection(this.properties, this.propCount, names, comparer);
        } 

         ///  
        ///     
        ///       Sorts the members of this PropertyDescriptorCollection, using the specified IComparer to compare,
        ///       the PropertyDescriptors contained in the collection. 
        ///    
        /// 
        public virtual PropertyDescriptorCollection Sort(IComparer comparer) {
            return new PropertyDescriptorCollection(this.properties, this.propCount, this.namedSort, comparer); 
        }
 
        ///  
        ///    
        ///       Sorts the members of this PropertyDescriptorCollection.  Any specified NamedSort arguments will 
        ///       be applied first, followed by sort using the specified IComparer.
        ///    
        /// 
        protected void InternalSort(string[] names) { 
            if (properties == null || properties.Length == 0) {
                return; 
            } 

            this.InternalSort(this.comparer); 

            if (names != null && names.Length > 0) {

               ArrayList propArrayList = new ArrayList(properties); 
               int foundCount = 0;
               int propCount = properties.Length; 
 
               for (int i = 0; i < names.Length; i++) {
                    for (int j = 0; j < propCount; j++) { 
                        PropertyDescriptor currentProp = (PropertyDescriptor)propArrayList[j];

                        // Found a matching property.  Here, we add it to our array.  We also
                        // mark it as null in our array list so we don't add it twice later. 
                        //
                        if (currentProp != null && currentProp.Name.Equals(names[i])) { 
                            properties[foundCount++] = currentProp; 
                            propArrayList[j] = null;
                            break; 
                        }
                    }
               }
 
               // At this point we have filled in the first "foundCount" number of propeties, one for each
               // name in our name array.  If a name didn't match, then it is ignored.  Next, we must fill 
               // in the rest of the properties.  We now have a sparse array containing the remainder, so 
               // it's easy.
               // 
               for (int i = 0; i < propCount; i++) {
                   if (propArrayList[i] != null) {
                       properties[foundCount++] = (PropertyDescriptor)propArrayList[i];
                   } 
               }
 
               Debug.Assert(foundCount == propCount, "We did not completely fill our property array"); 
            }
        } 

        /// 
        ///    
        ///       Sorts the members of this PropertyDescriptorCollection using the specified IComparer. 
        ///    
        ///  
        protected void InternalSort(IComparer sorter) { 
            if (sorter == null) {
                TypeDescriptor.SortDescriptorArray(this); 
            }
            else {
                Array.Sort(properties, sorter);
            } 
        }
 
        ///  
        ///    
        ///       Gets an enumerator for this . 
        ///    
        /// 
        public virtual IEnumerator GetEnumerator() {
            EnsurePropsOwned(); 
            // we can only return an enumerator on the props we actually have...
            if (properties.Length != this.propCount) { 
                PropertyDescriptor[] enumProps = new PropertyDescriptor[propCount]; 
                Array.Copy(properties, 0, enumProps, 0, propCount);
                return enumProps.GetEnumerator(); 
            }
            return properties.GetEnumerator();
        }
 
        /// 
        int ICollection.Count { 
            get { 
                return Count;
            } 
        }

        /// 
        bool ICollection.IsSynchronized { 
            get {
                return false; 
            } 
        }
 
        /// 
        object ICollection.SyncRoot {
            get {
                return null; 
            }
        } 
 
        /// 
        void IDictionary.Add(object key, object value) { 
            PropertyDescriptor newProp = value as PropertyDescriptor;

            if (newProp == null) {
                throw new ArgumentException("value"); 
            }
            Add(newProp); 
        } 

        ///  
        void IDictionary.Clear() {
            Clear();
        }
 
        /// 
        bool IDictionary.Contains(object key) { 
            if (key is string) { 
                return this[(string)key] != null;
            } 
            return false;
        }

        ///  
        IDictionaryEnumerator IDictionary.GetEnumerator() {
            return new PropertyDescriptorEnumerator(this); 
        } 

         ///  
         bool IDictionary.IsFixedSize {
            get {
                return readOnly;
            } 
        }
 
        ///  
        bool IDictionary.IsReadOnly {
            get { 
                return readOnly;
            }
        }
 
        /// 
        object IDictionary.this[object key] { 
            get { 
                if (key is string) {
                    return this[(string)key]; 
                }
                return null;
            }
 
            set {
                if (readOnly) { 
                    throw new NotSupportedException(); 
                }
 
                if (value != null && !(value is PropertyDescriptor)) {
                    throw new ArgumentException("value");
                }
 
                int index = -1;
 
                if (key is int) { 
                    index = (int)key;
 
                    if (index < 0 || index >= propCount) {
                        throw new IndexOutOfRangeException();
                    }
                } 
                else if (key is string) {
                    for (int i = 0; i < propCount; i++) { 
                        if (properties[i].Name.Equals((string)key)) { 
                            index = i;
                            break; 
                        }
                    }
                }
                else { 
                    throw new ArgumentException("key");
                } 
 
                if (index == -1) {
                    Add((PropertyDescriptor)value); 
                }
                else {
                    EnsurePropsOwned();
                    properties[index] = (PropertyDescriptor)value; 
                    if (cachedFoundProperties != null && key is string) {
                        cachedFoundProperties[key] = value; 
                    } 
                }
            } 
        }

        /// 
        ICollection IDictionary.Keys { 
            get {
                string[] keys = new string[propCount]; 
                for (int i = 0; i < propCount ;i++) { 
                    keys[i] = properties[i].Name;
                } 
                return keys;
            }
        }
 
        /// 
        ICollection IDictionary.Values { 
            get { 
                // we can only return an enumerator on the props we actually have...
                // 
                if (properties.Length != this.propCount) {
                    PropertyDescriptor[] newProps = new PropertyDescriptor[propCount];
                    Array.Copy(properties, 0, newProps, 0, propCount);
                    return newProps; 
                }
                else { 
                    return (ICollection)properties.Clone(); 
                }
            } 
        }

        /// 
        void IDictionary.Remove(object key) { 
            if (key is string) {
                PropertyDescriptor pd = this[(string)key]; 
                if (pd != null) { 
                    ((IList)this).Remove(pd);
                } 
            }
        }

        ///  
        IEnumerator IEnumerable.GetEnumerator() {
            return GetEnumerator(); 
        } 

        ///  
        int IList.Add(object value) {
            return Add((PropertyDescriptor)value);
        }
 
        /// 
        void IList.Clear() { 
            Clear(); 
        }
 
        /// 
        bool IList.Contains(object value) {
            return Contains((PropertyDescriptor)value);
        } 

        ///  
        int IList.IndexOf(object value) { 
            return IndexOf((PropertyDescriptor)value);
        } 

        /// 
        void IList.Insert(int index, object value) {
            Insert(index, (PropertyDescriptor)value); 
        }
 
        ///  
        bool IList.IsReadOnly {
            get { 
                return readOnly;
            }
        }
 
        /// 
        bool IList.IsFixedSize { 
            get { 
                return readOnly;
            } 
        }

        /// 
        void IList.Remove(object value) { 
            Remove((PropertyDescriptor)value);
        } 
 
        /// 
        void IList.RemoveAt(int index) { 
            RemoveAt(index);
        }

        ///  
        object IList.this[int index] {
            get { 
                return this[index]; 
            }
            set { 
                if (readOnly) {
                    throw new NotSupportedException();
                }
 
                if (index >= propCount) {
                    throw new IndexOutOfRangeException(); 
                } 

 
                if (value != null && !(value is PropertyDescriptor)) {
                    throw new ArgumentException("value");
                }
 
                EnsurePropsOwned();
                properties[index] = (PropertyDescriptor)value; 
            } 
        }
 
        private class PropertyDescriptorEnumerator : IDictionaryEnumerator {
            private PropertyDescriptorCollection owner;
            private int index = -1;
 
            public PropertyDescriptorEnumerator(PropertyDescriptorCollection owner) {
                this.owner = owner; 
            } 

            public object Current { 
                get{
                    return Entry;
                }
            } 

            public DictionaryEntry Entry { 
                get { 
                    PropertyDescriptor curProp = owner[index];
                    return new DictionaryEntry(curProp.Name, curProp); 
                }
            }

            public object Key { 
                get {
                    return owner[index].Name; 
                } 
            }
 
            public object Value {
                get {
                    return owner[index].Name;
                } 
            }
 
            public bool MoveNext() { 
                if (index < (owner.Count - 1)) {
                    index++; 
                    return true;
                }
                return false;
            } 

            public void Reset() { 
                index = -1; 
            }
        } 
    }
}


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

using System.Diagnostics.CodeAnalysis; 
 
[assembly: SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.Add(System.Object,System.Object):System.Void")]
[assembly: SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.set_Item(System.Int32,System.Object):System.Void")] 
[assembly: SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.set_Item(System.Object,System.Object):System.Void")]

/*
 This class has the HostProtectionAttribute. The purpose of this attribute is to enforce host-specific programming model guidelines, not security behavior. 
 Suppress FxCop message - BUT REVISIT IF ADDING NEW SECURITY ATTRIBUTES.
*/ 
[assembly: SuppressMessage("Microsoft.Security", "CA2112:SecuredTypesShouldNotExposeFields", Scope="type", Target="System.ComponentModel.PropertyDescriptorCollection")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.get_IsFixedSize():System.Boolean")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IEnumerable.GetEnumerator():System.Collections.IEnumerator")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.get_IsFixedSize():System.Boolean")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.ICollection.get_SyncRoot():System.Object")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.Remove(System.Object):System.Void")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.ICollection.get_IsSynchronized():System.Boolean")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.get_IsReadOnly():System.Boolean")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.get_Keys():System.Collections.ICollection")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.get_IsReadOnly():System.Boolean")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.Clear():System.Void")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.IndexOf(System.Object):System.Int32")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.Contains(System.Object):System.Boolean")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.RemoveAt(System.Int32):System.Void")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.GetEnumerator():System.Collections.IEnumerator")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.Add(System.Object,System.Object):System.Void")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.Clear():System.Void")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.get_Values():System.Collections.ICollection")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.Contains(System.Object):System.Boolean")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.GetEnumerator():System.Collections.IDictionaryEnumerator")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.Remove(System.Object):System.Void")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.get_Item(System.Int32):System.Object")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.set_Item(System.Int32,System.Object):System.Void")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.Add(System.Object):System.Int32")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.CopyTo(System.Array,System.Int32):System.Void")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.get_Item(System.Object):System.Object")]
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IDictionary.set_Item(System.Object,System.Object):System.Void")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.IList.Insert(System.Int32,System.Object):System.Void")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.System.Collections.ICollection.get_Count():System.Int32")]
[assembly: SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection..ctor(System.ComponentModel.PropertyDescriptor[])")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.get_Count():System.Int32")]
[assembly: SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.get_Item(System.String):System.ComponentModel.PropertyDescriptor")]
[assembly: SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection..ctor(System.ComponentModel.PropertyDescriptor[],System.Boolean)")]
[assembly: SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.GetEnumerator():System.Collections.IEnumerator")] 
[assembly: SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.Find(System.String,System.Boolean):System.ComponentModel.PropertyDescriptor")]
[assembly: SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Scope="member", Target="System.ComponentModel.PropertyDescriptorCollection.Sort(System.String[]):System.ComponentModel.PropertyDescriptorCollection")] 
 
namespace System.ComponentModel {
    using Microsoft.Win32; 
    using System.Collections;
    using System.Collections.Specialized;
    using System.Diagnostics;
    using System.Globalization; 
    using System.Runtime.InteropServices;
    using System.Security.Permissions; 
 
    /// 
    ///     
    ///       Represents a collection of properties.
    ///    
    /// 
    [System.Security.Permissions.HostProtection(Synchronization = true)] 
    public class PropertyDescriptorCollection : ICollection, IList, IDictionary {
 
 
        /// 
        /// An empty PropertyDescriptorCollection that can used instead of creating a new one with no items. 
        /// 
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2112:SecuredTypesShouldNotExposeFields")] // ReadOnly fields - already shipped.
        public static readonly PropertyDescriptorCollection Empty = new PropertyDescriptorCollection(null, true);
 
        private IDictionary          cachedFoundProperties;
        private bool                 cachedIgnoreCase; 
        private PropertyDescriptor[] properties; 
        private int                  propCount = 0;
        private string[]             namedSort; 
        private IComparer            comparer;
        private bool                 propsOwned = true;
        private bool                 needSort = false;
        private bool                 readOnly = false; 

        ///  
        ///     
        ///       Initializes a new instance of the 
        ///       class. 
        ///    
        /// 
        public PropertyDescriptorCollection(PropertyDescriptor[] properties) {
            this.properties = properties; 
            if (properties == null) {
                this.properties = new PropertyDescriptor[0]; 
                this.propCount = 0; 
            }
            else { 
                this.propCount = properties.Length;
            }
            this.propsOwned = true;
        } 

        ///  
        ///     Initializes a new instance of a property descriptor collection, and allows you to mark the 
        ///     collection as read-only so it cannot be modified.
        ///  
        public PropertyDescriptorCollection(PropertyDescriptor[] properties, bool readOnly) : this(properties) {
            this.readOnly = readOnly;
        }
 
        private PropertyDescriptorCollection(PropertyDescriptor[] properties, int propCount, string[] namedSort, IComparer comparer) {
            this.propsOwned = false; 
            if (namedSort != null) { 
               this.namedSort = (string[])namedSort.Clone();
            } 
            this.comparer = comparer;
            this.properties = properties;
            this.propCount = propCount;
            this.needSort = true; 
        }
 
        ///  
        ///    
        ///       Gets the number 
        ///       of property descriptors in the
        ///       collection.
        ///    
        ///  
        public int Count {
            get { 
                return propCount; 
            }
        } 

        /// 
        ///    Gets the property with the specified index
        ///       number. 
        /// 
        public virtual PropertyDescriptor this[int index] { 
            get { 
                if (index >= propCount) {
                    throw new IndexOutOfRangeException(); 
                }
                EnsurePropsOwned();
                return properties[index];
            } 
        }
 
        ///  
        ///    Gets the property with the specified name.
        ///  
        public virtual PropertyDescriptor this[string name] {
            get {
                return Find(name, false);
            } 
        }
 
        ///  
        ///    [To be supplied.]
        ///  
        public int Add(PropertyDescriptor value) {
            if (readOnly) {
                throw new NotSupportedException();
            } 

            EnsureSize(propCount + 1); 
            properties[propCount++] = value; 
            return propCount - 1;
        } 

        /// 
        ///    [To be supplied.]
        ///  
        public void Clear() {
            if (readOnly) { 
                throw new NotSupportedException(); 
            }
 
            propCount = 0;
            cachedFoundProperties = null;
        }
 
        /// 
        ///    [To be supplied.] 
        ///  
        public bool Contains(PropertyDescriptor value) {
            return IndexOf(value) >= 0; 
        }

        /// 
        ///    [To be supplied.] 
        /// 
        public void CopyTo(Array array, int index) { 
            EnsurePropsOwned(); 
            Array.Copy(properties, 0, array, index, Count);
        } 

        private void EnsurePropsOwned() {
            if (!propsOwned) {
               propsOwned = true; 
               if (properties != null) {
                  PropertyDescriptor[] newProps = new PropertyDescriptor[Count]; 
                  Array.Copy(properties, 0, newProps, 0, Count); 
                  this.properties = newProps;
               } 
            }

            if (needSort) {
               needSort = false; 
               InternalSort(this.namedSort);
            } 
        } 

        private void EnsureSize(int sizeNeeded) { 

            if (sizeNeeded <= properties.Length) {
               return;
            } 

            if (properties == null || properties.Length == 0) { 
                propCount = 0; 
                properties = new PropertyDescriptor[sizeNeeded];
                return; 
            }

            EnsurePropsOwned();
 
            int newSize = Math.Max(sizeNeeded, properties.Length * 2);
            PropertyDescriptor[] newProps = new PropertyDescriptor[newSize]; 
            Array.Copy(properties, 0, newProps, 0, propCount); 
            properties = newProps;
        } 

        /// 
        ///    Gets the description of the property with the specified
        ///       name. 
        /// 
        public virtual PropertyDescriptor Find(string name, bool ignoreCase) { 
            lock(this) { 
                PropertyDescriptor p = null;
 
                if (cachedFoundProperties == null || cachedIgnoreCase != ignoreCase) {
                    cachedIgnoreCase = ignoreCase;
                    cachedFoundProperties = new HybridDictionary(ignoreCase);
                } 

                // first try to find it in the cache 
                // 
                object cached = cachedFoundProperties[name];
 
                if (cached != null) {
                    return (PropertyDescriptor) cached;
                }
 
                // Now start walking from where we last left off, filling
                // the cache as we go. 
                // 
                for(int i = 0; i < propCount; i++) {
 
                    if (ignoreCase) {
                        if (String.Equals(properties[i].Name, name, StringComparison.OrdinalIgnoreCase)) {
                            cachedFoundProperties[name] = properties[i];
                            p = properties[i]; 
                            break;
                        } 
                    } 
                    else {
                        if (properties[i].Name.Equals(name)) { 
                            cachedFoundProperties[name] = properties[i];
                            p = properties[i];
                            break;
                        } 
                    }
                } 
 
                return p;
            } 
        }

        /// 
        ///    [To be supplied.] 
        /// 
        public int IndexOf(PropertyDescriptor value) { 
            return Array.IndexOf(properties, value, 0, propCount); 
        }
 
        /// 
        ///    [To be supplied.]
        /// 
        public void Insert(int index, PropertyDescriptor value) { 
            if (readOnly) {
                throw new NotSupportedException(); 
            } 

            EnsureSize(propCount + 1); 
            if (index < propCount) {
                Array.Copy(properties, index, properties, index + 1, propCount - index);
            }
            properties[index] = value; 
            propCount++;
        } 
 
        /// 
        ///    [To be supplied.] 
        /// 
        public void Remove(PropertyDescriptor value) {
            if (readOnly) {
                throw new NotSupportedException(); 
            }
 
            int index = IndexOf(value); 

            if (index != -1) { 
                RemoveAt(index);
            }
        }
 
        /// 
        ///    [To be supplied.] 
        ///  
        public void RemoveAt(int index) {
            if (readOnly) { 
                throw new NotSupportedException();
            }

            if (index < propCount - 1) { 
                Array.Copy(properties, index + 1, properties, index, propCount - index - 1);
            } 
            properties[propCount - 1] = null; 
            propCount--;
        } 

        /// 
        ///    
        ///       Sorts the members of this PropertyDescriptorCollection, using the default sort for this collection, 
        ///       which is usually alphabetical.
        ///     
        ///  
        public virtual PropertyDescriptorCollection Sort() {
            return new PropertyDescriptorCollection(this.properties, this.propCount, this.namedSort, this.comparer); 
        }


        ///  
        ///    
        ///       Sorts the members of this PropertyDescriptorCollection.  Any specified NamedSort arguments will 
        ///       be applied first, followed by sort using the specified IComparer. 
        ///    
        ///  
        public virtual PropertyDescriptorCollection Sort(string[] names) {
            return new PropertyDescriptorCollection(this.properties, this.propCount, names, this.comparer);
        }
 
        /// 
        ///     
        ///       Sorts the members of this PropertyDescriptorCollection.  Any specified NamedSort arguments will 
        ///       be applied first, followed by sort using the specified IComparer.
        ///     
        /// 
        public virtual PropertyDescriptorCollection Sort(string[] names, IComparer comparer) {
            return new PropertyDescriptorCollection(this.properties, this.propCount, names, comparer);
        } 

         ///  
        ///     
        ///       Sorts the members of this PropertyDescriptorCollection, using the specified IComparer to compare,
        ///       the PropertyDescriptors contained in the collection. 
        ///    
        /// 
        public virtual PropertyDescriptorCollection Sort(IComparer comparer) {
            return new PropertyDescriptorCollection(this.properties, this.propCount, this.namedSort, comparer); 
        }
 
        ///  
        ///    
        ///       Sorts the members of this PropertyDescriptorCollection.  Any specified NamedSort arguments will 
        ///       be applied first, followed by sort using the specified IComparer.
        ///    
        /// 
        protected void InternalSort(string[] names) { 
            if (properties == null || properties.Length == 0) {
                return; 
            } 

            this.InternalSort(this.comparer); 

            if (names != null && names.Length > 0) {

               ArrayList propArrayList = new ArrayList(properties); 
               int foundCount = 0;
               int propCount = properties.Length; 
 
               for (int i = 0; i < names.Length; i++) {
                    for (int j = 0; j < propCount; j++) { 
                        PropertyDescriptor currentProp = (PropertyDescriptor)propArrayList[j];

                        // Found a matching property.  Here, we add it to our array.  We also
                        // mark it as null in our array list so we don't add it twice later. 
                        //
                        if (currentProp != null && currentProp.Name.Equals(names[i])) { 
                            properties[foundCount++] = currentProp; 
                            propArrayList[j] = null;
                            break; 
                        }
                    }
               }
 
               // At this point we have filled in the first "foundCount" number of propeties, one for each
               // name in our name array.  If a name didn't match, then it is ignored.  Next, we must fill 
               // in the rest of the properties.  We now have a sparse array containing the remainder, so 
               // it's easy.
               // 
               for (int i = 0; i < propCount; i++) {
                   if (propArrayList[i] != null) {
                       properties[foundCount++] = (PropertyDescriptor)propArrayList[i];
                   } 
               }
 
               Debug.Assert(foundCount == propCount, "We did not completely fill our property array"); 
            }
        } 

        /// 
        ///    
        ///       Sorts the members of this PropertyDescriptorCollection using the specified IComparer. 
        ///    
        ///  
        protected void InternalSort(IComparer sorter) { 
            if (sorter == null) {
                TypeDescriptor.SortDescriptorArray(this); 
            }
            else {
                Array.Sort(properties, sorter);
            } 
        }
 
        ///  
        ///    
        ///       Gets an enumerator for this . 
        ///    
        /// 
        public virtual IEnumerator GetEnumerator() {
            EnsurePropsOwned(); 
            // we can only return an enumerator on the props we actually have...
            if (properties.Length != this.propCount) { 
                PropertyDescriptor[] enumProps = new PropertyDescriptor[propCount]; 
                Array.Copy(properties, 0, enumProps, 0, propCount);
                return enumProps.GetEnumerator(); 
            }
            return properties.GetEnumerator();
        }
 
        /// 
        int ICollection.Count { 
            get { 
                return Count;
            } 
        }

        /// 
        bool ICollection.IsSynchronized { 
            get {
                return false; 
            } 
        }
 
        /// 
        object ICollection.SyncRoot {
            get {
                return null; 
            }
        } 
 
        /// 
        void IDictionary.Add(object key, object value) { 
            PropertyDescriptor newProp = value as PropertyDescriptor;

            if (newProp == null) {
                throw new ArgumentException("value"); 
            }
            Add(newProp); 
        } 

        ///  
        void IDictionary.Clear() {
            Clear();
        }
 
        /// 
        bool IDictionary.Contains(object key) { 
            if (key is string) { 
                return this[(string)key] != null;
            } 
            return false;
        }

        ///  
        IDictionaryEnumerator IDictionary.GetEnumerator() {
            return new PropertyDescriptorEnumerator(this); 
        } 

         ///  
         bool IDictionary.IsFixedSize {
            get {
                return readOnly;
            } 
        }
 
        ///  
        bool IDictionary.IsReadOnly {
            get { 
                return readOnly;
            }
        }
 
        /// 
        object IDictionary.this[object key] { 
            get { 
                if (key is string) {
                    return this[(string)key]; 
                }
                return null;
            }
 
            set {
                if (readOnly) { 
                    throw new NotSupportedException(); 
                }
 
                if (value != null && !(value is PropertyDescriptor)) {
                    throw new ArgumentException("value");
                }
 
                int index = -1;
 
                if (key is int) { 
                    index = (int)key;
 
                    if (index < 0 || index >= propCount) {
                        throw new IndexOutOfRangeException();
                    }
                } 
                else if (key is string) {
                    for (int i = 0; i < propCount; i++) { 
                        if (properties[i].Name.Equals((string)key)) { 
                            index = i;
                            break; 
                        }
                    }
                }
                else { 
                    throw new ArgumentException("key");
                } 
 
                if (index == -1) {
                    Add((PropertyDescriptor)value); 
                }
                else {
                    EnsurePropsOwned();
                    properties[index] = (PropertyDescriptor)value; 
                    if (cachedFoundProperties != null && key is string) {
                        cachedFoundProperties[key] = value; 
                    } 
                }
            } 
        }

        /// 
        ICollection IDictionary.Keys { 
            get {
                string[] keys = new string[propCount]; 
                for (int i = 0; i < propCount ;i++) { 
                    keys[i] = properties[i].Name;
                } 
                return keys;
            }
        }
 
        /// 
        ICollection IDictionary.Values { 
            get { 
                // we can only return an enumerator on the props we actually have...
                // 
                if (properties.Length != this.propCount) {
                    PropertyDescriptor[] newProps = new PropertyDescriptor[propCount];
                    Array.Copy(properties, 0, newProps, 0, propCount);
                    return newProps; 
                }
                else { 
                    return (ICollection)properties.Clone(); 
                }
            } 
        }

        /// 
        void IDictionary.Remove(object key) { 
            if (key is string) {
                PropertyDescriptor pd = this[(string)key]; 
                if (pd != null) { 
                    ((IList)this).Remove(pd);
                } 
            }
        }

        ///  
        IEnumerator IEnumerable.GetEnumerator() {
            return GetEnumerator(); 
        } 

        ///  
        int IList.Add(object value) {
            return Add((PropertyDescriptor)value);
        }
 
        /// 
        void IList.Clear() { 
            Clear(); 
        }
 
        /// 
        bool IList.Contains(object value) {
            return Contains((PropertyDescriptor)value);
        } 

        ///  
        int IList.IndexOf(object value) { 
            return IndexOf((PropertyDescriptor)value);
        } 

        /// 
        void IList.Insert(int index, object value) {
            Insert(index, (PropertyDescriptor)value); 
        }
 
        ///  
        bool IList.IsReadOnly {
            get { 
                return readOnly;
            }
        }
 
        /// 
        bool IList.IsFixedSize { 
            get { 
                return readOnly;
            } 
        }

        /// 
        void IList.Remove(object value) { 
            Remove((PropertyDescriptor)value);
        } 
 
        /// 
        void IList.RemoveAt(int index) { 
            RemoveAt(index);
        }

        ///  
        object IList.this[int index] {
            get { 
                return this[index]; 
            }
            set { 
                if (readOnly) {
                    throw new NotSupportedException();
                }
 
                if (index >= propCount) {
                    throw new IndexOutOfRangeException(); 
                } 

 
                if (value != null && !(value is PropertyDescriptor)) {
                    throw new ArgumentException("value");
                }
 
                EnsurePropsOwned();
                properties[index] = (PropertyDescriptor)value; 
            } 
        }
 
        private class PropertyDescriptorEnumerator : IDictionaryEnumerator {
            private PropertyDescriptorCollection owner;
            private int index = -1;
 
            public PropertyDescriptorEnumerator(PropertyDescriptorCollection owner) {
                this.owner = owner; 
            } 

            public object Current { 
                get{
                    return Entry;
                }
            } 

            public DictionaryEntry Entry { 
                get { 
                    PropertyDescriptor curProp = owner[index];
                    return new DictionaryEntry(curProp.Name, curProp); 
                }
            }

            public object Key { 
                get {
                    return owner[index].Name; 
                } 
            }
 
            public object Value {
                get {
                    return owner[index].Name;
                } 
            }
 
            public bool MoveNext() { 
                if (index < (owner.Count - 1)) {
                    index++; 
                    return true;
                }
                return false;
            } 

            public void Reset() { 
                index = -1; 
            }
        } 
    }
}


// File provided for Reference Use Only by Microsoft Corporation (c) 2007.

                        

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