Exemplo n.º 1
0
        /// <include file='doc\GridEntry.uex' path='docs/doc[@for="GridEntry.GetPropEntries"]/*' />
        /// <devdoc>
        /// Returns the child GridEntries for this item.
        /// </devdoc>
        protected virtual GridEntry[] GetPropEntries(GridEntry peParent, object obj, Type objType) {


            // we don't want to create subprops for null objects.
            if (obj == null) {
                return null;
            }

            GridEntry[] entries = null;

            Attribute[] attributes = new Attribute[this.BrowsableAttributes.Count];
            this.BrowsableAttributes.CopyTo(attributes, 0);

            PropertyTab tab = this.CurrentTab;
            Debug.Assert(tab != null, "No current tab!");

            try
            {

                bool forceReadOnly = this.ForceReadOnly;

                if (!forceReadOnly)
                {
                    ReadOnlyAttribute readOnlyAttr = (ReadOnlyAttribute)TypeDescriptor.GetAttributes(obj)[typeof(ReadOnlyAttribute)];
                    forceReadOnly = (readOnlyAttr != null && !readOnlyAttr.IsDefaultAttribute());
                }

                // do we want to expose sub properties?
                //
                if (TypeConverter.GetPropertiesSupported(this) || AlwaysAllowExpand)
                {

                    // ask the tab if we have one.
                    //
                    PropertyDescriptorCollection props = null;
                    PropertyDescriptor defProp = null;
                    if (tab != null)
                    {
                        props = tab.GetProperties(this, obj, attributes);
                        defProp = tab.GetDefaultProperty(obj);
                    }
                    else
                    {
                        props = TypeConverter.GetProperties(this, obj, attributes);
                        defProp = TypeDescriptor.GetDefaultProperty(obj);
                    }

                    if (props == null)
                    {
                        return null;
                    }

                    if ((this.PropertySort & PropertySort.Alphabetical) != 0)
                    {
                        if (objType == null || !objType.IsArray)
                        {
                            props = props.Sort(GridEntry.DisplayNameComparer);
                        }

                        PropertyDescriptor[] propertyDescriptors = new PropertyDescriptor[props.Count];
                        props.CopyTo(propertyDescriptors, 0);

                        props = new PropertyDescriptorCollection(SortParenProperties(propertyDescriptors));
                    }

                    if (defProp == null && props.Count > 0)
                    {
                        defProp = props[0];
                    }

                    // if the target object is an array and nothing else has provided a set of
                    // properties to use, then expand the array.
                    //
                    if ((props == null || props.Count == 0) && objType != null && objType.IsArray && obj != null)
                    {

                        Array objArray = (Array)obj;

                        entries = new GridEntry[objArray.Length];

                        for (int i = 0; i < entries.Length; i++)
                        {
                            entries[i] = new ArrayElementGridEntry(this.ownerGrid, peParent, i);
                        }
                    }
                    else
                    {
                        // otherwise, create the proper GridEntries.
                        // 
                        bool createInstanceSupported = TypeConverter.GetCreateInstanceSupported(this);
                        entries = new GridEntry[props.Count];
                        int index = 0;

                        // loop through all the props we got and create property descriptors.
                        //
                        foreach (PropertyDescriptor pd in props)
                        {
                            GridEntry newEntry;

                            // make sure we've got a valid property, otherwise hide it
                            //
                            bool hide = false;
                            try
                            {
                                object owner = obj;
                                if (obj is ICustomTypeDescriptor)
                                {
                                    owner = ((ICustomTypeDescriptor)obj).GetPropertyOwner(pd);
                                }
                                pd.GetValue(owner);
                            }
                            catch (Exception w)
                            {
                                if (PbrsAssertPropsSwitch.Enabled)
                                {
                                    Debug.Fail("Bad property '" + peParent.PropertyLabel + "." + pd.Name + "': " + w.ToString());
                                }
                                hide = true;
                            }

                            if (createInstanceSupported)
                            {
                                newEntry = new ImmutablePropertyDescriptorGridEntry(this.ownerGrid, peParent, pd, hide);
                            }
                            else
                            {
                                newEntry = new PropertyDescriptorGridEntry(this.ownerGrid, peParent, pd, hide);
                            }

                            if (forceReadOnly)
                            {
                                newEntry.flags |= FLAG_FORCE_READONLY;
                            }

                            // check to see if we've come across the default item.
                            //
                            if (pd.Equals(defProp))
                            {
                                this.DefaultChild = newEntry;
                            }

                            // add it to the array.
                            //
                            entries[index++] = newEntry;
                        }
                    }
                }
            }
            catch (Exception e)
            {
#if DEBUG
                if (PbrsAssertPropsSwitch.Enabled) {
                    // Checked builds are not giving us enough information here.  So, output as much stuff as
                    // we can.
                    System.Text.StringBuilder b = new System.Text.StringBuilder();
                    b.Append(string.Format(CultureInfo.CurrentCulture, "********* Debug log written on {0} ************\r\n", DateTime.Now));
                    b.Append(string.Format(CultureInfo.CurrentCulture, "Exception '{0}' reading properties for object {1}.\r\n", e.GetType().Name, obj));
                    b.Append(string.Format(CultureInfo.CurrentCulture, "Exception Text: \r\n{0}", e.ToString()));
                    b.Append(string.Format(CultureInfo.CurrentCulture, "Exception stack: \r\n{0}", e.StackTrace));
                    string path = string.Format(CultureInfo.CurrentCulture, "{0}\\PropertyGrid.log", Environment.GetEnvironmentVariable("SYSTEMDRIVE"));
                    System.IO.FileStream s = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.None);
                    System.IO.StreamWriter w = new System.IO.StreamWriter(s);
                    w.Write(b.ToString());
                    w.Close();
                    s.Close();
                    RTLAwareMessageBox.Show(null, b.ToString(), SR.GetString(SR.PropertyGridInternalNoProp, path),
                        MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
                }
#endif
                Debug.Fail("Failed to get properties: " + e.GetType().Name + "," + e.Message + "\n" + e.StackTrace);
            }
            return entries;
        }
 protected virtual GridEntry[] GetPropEntries(GridEntry peParent, object obj, System.Type objType)
 {
     if (obj == null)
     {
         return null;
     }
     GridEntry[] entryArray = null;
     Attribute[] attributeArray = new Attribute[this.BrowsableAttributes.Count];
     this.BrowsableAttributes.CopyTo(attributeArray, 0);
     PropertyTab currentTab = this.CurrentTab;
     try
     {
         bool forceReadOnly = this.ForceReadOnly;
         if (!forceReadOnly)
         {
             ReadOnlyAttribute attribute = (ReadOnlyAttribute) TypeDescriptor.GetAttributes(obj)[typeof(ReadOnlyAttribute)];
             forceReadOnly = (attribute != null) && !attribute.IsDefaultAttribute();
         }
         if (!this.TypeConverter.GetPropertiesSupported(this) && !this.AlwaysAllowExpand)
         {
             return entryArray;
         }
         PropertyDescriptorCollection descriptors = null;
         System.ComponentModel.PropertyDescriptor defaultProperty = null;
         if (currentTab != null)
         {
             descriptors = currentTab.GetProperties(this, obj, attributeArray);
             defaultProperty = currentTab.GetDefaultProperty(obj);
         }
         else
         {
             descriptors = this.TypeConverter.GetProperties(this, obj, attributeArray);
             defaultProperty = TypeDescriptor.GetDefaultProperty(obj);
         }
         if (descriptors == null)
         {
             return null;
         }
         if ((this.PropertySort & System.Windows.Forms.PropertySort.Alphabetical) != System.Windows.Forms.PropertySort.NoSort)
         {
             if ((objType == null) || !objType.IsArray)
             {
                 descriptors = descriptors.Sort(DisplayNameComparer);
             }
             System.ComponentModel.PropertyDescriptor[] descriptorArray = new System.ComponentModel.PropertyDescriptor[descriptors.Count];
             descriptors.CopyTo(descriptorArray, 0);
             descriptors = new PropertyDescriptorCollection(this.SortParenProperties(descriptorArray));
         }
         if ((defaultProperty == null) && (descriptors.Count > 0))
         {
             defaultProperty = descriptors[0];
         }
         if (((descriptors == null) || (descriptors.Count == 0)) && (((objType != null) && objType.IsArray) && (obj != null)))
         {
             Array array = (Array) obj;
             entryArray = new GridEntry[array.Length];
             for (int i = 0; i < entryArray.Length; i++)
             {
                 entryArray[i] = new ArrayElementGridEntry(this.ownerGrid, peParent, i);
             }
             return entryArray;
         }
         bool createInstanceSupported = this.TypeConverter.GetCreateInstanceSupported(this);
         entryArray = new GridEntry[descriptors.Count];
         int num2 = 0;
         foreach (System.ComponentModel.PropertyDescriptor descriptor2 in descriptors)
         {
             GridEntry entry;
             bool hide = false;
             try
             {
                 object component = obj;
                 if (obj is ICustomTypeDescriptor)
                 {
                     component = ((ICustomTypeDescriptor) obj).GetPropertyOwner(descriptor2);
                 }
                 descriptor2.GetValue(component);
             }
             catch (Exception)
             {
                 bool enabled = PbrsAssertPropsSwitch.Enabled;
                 hide = true;
             }
             if (createInstanceSupported)
             {
                 entry = new ImmutablePropertyDescriptorGridEntry(this.ownerGrid, peParent, descriptor2, hide);
             }
             else
             {
                 entry = new PropertyDescriptorGridEntry(this.ownerGrid, peParent, descriptor2, hide);
             }
             if (forceReadOnly)
             {
                 entry.flags |= 0x400;
             }
             if (descriptor2.Equals(defaultProperty))
             {
                 this.DefaultChild = entry;
             }
             entryArray[num2++] = entry;
         }
     }
     catch (Exception)
     {
     }
     return entryArray;
 }