/// <summary>
        /// Creates property items for all properties in the specified object.
        /// </summary>
        /// <param name="instance">The object instance.</param>
        /// <param name="options">The options.</param>
        /// <returns>
        /// Enumeration of PropertyItem.
        /// </returns>
        protected virtual IEnumerable <PropertyItem> CreatePropertyItems(object instance, IPropertyGridOptions options)
        {
            var instanceType = instance.GetType();

            var properties = TypeDescriptor.GetProperties(instance);

            foreach (PropertyDescriptor pd in properties)
            {
                if (options.ShowDeclaredOnly && pd.ComponentType != instanceType)
                {
                    continue;
                }

                // Skip properties marked with [System.ComponentModel.Browsable(false)] or [PropertyTools.DataAnnotations.Browsable(false)]
                if (!pd.IsBrowsable())
                {
                    continue;
                }

                // Read-only properties
                if (!options.ShowReadOnlyProperties && pd.IsReadOnly())
                {
                    continue;
                }

                // If RequiredAttribute is set, skip properties that don't have the given attribute
                if (options.RequiredAttribute != null && pd.GetFirstAttributeOrDefault(options.RequiredAttribute) == null)
                {
                    continue;
                }

                yield return(this.CreatePropertyItem(pd, properties, instance));
            }
        }
        /// <summary>
        /// Creates the property model.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="isEnumerable">if set to <c>true</c> [is enumerable].</param>
        /// <param name="options">The options.</param>
        /// <returns>
        /// A list of <see cref="Tab" /> .
        /// </returns>
        public virtual IEnumerable <Tab> CreateModel(object instance, bool isEnumerable, IPropertyGridOptions options)
        {
            if (instance == null)
            {
                return(null);
            }

            this.Reset();

            var tabs = new Dictionary <string, Tab>();

            foreach (var pi in this.CreatePropertyItems(instance, options).OrderBy(t => t.SortIndex))
            {
                var tabHeader = pi.Tab ?? string.Empty;
                if (!tabs.ContainsKey(tabHeader))
                {
                    tabs.Add(tabHeader, new Tab {
                        Header = pi.Tab
                    });
                }

                var tab   = tabs[tabHeader];
                var group = tab.Groups.FirstOrDefault(g => g.Header == pi.Category);
                if (group == null)
                {
                    group = new Group {
                        Header = pi.Category
                    };
                    tab.Groups.Add(group);
                }

                group.Properties.Add(pi);
            }

            return(tabs.Values.ToList());
        }
        /// <summary>
        /// Creates property items for all properties in the specified object.
        /// </summary>
        /// <param name="instance">The object instance.</param>
        /// <param name="options">The options.</param>
        /// <returns>
        /// Enumeration of PropertyItem.
        /// </returns>
        protected virtual IEnumerable <PropertyItem> CreatePropertyItems(object instance, IPropertyGridOptions options)
        {
            var instanceType = instance.GetType();

            // check if the MetadataTypeAttribute is set
            var metadataTypeAttribute = instanceType.GetCustomAttributes(typeof(MetadataTypeAttribute), true)
                                        .OfType <MetadataTypeAttribute>().FirstOrDefault();
            PropertyDescriptorCollection properties;

            if (metadataTypeAttribute != null)
            {
                // use the metadata type for reflection
                instanceType = metadataTypeAttribute.MetadataClassType;
                properties   = TypeDescriptor.GetProperties(instanceType);
            }
            else
            {
                properties = TypeDescriptor.GetProperties(instance);
            }

            foreach (PropertyDescriptor pd in properties)
            {
                if (options.ShowDeclaredOnly && pd.ComponentType != instanceType)
                {
                    continue;
                }

                // Skip properties marked with [PropertyTools.DataAnnotations.Browsable(false)]
                var ba = pd.GetFirstAttributeOrDefault <DataAnnotations.BrowsableAttribute>();
                if (ba != null && !ba.Browsable)
                {
                    continue;
                }

                // Skip properties marked with [System.ComponentModel.Browsable(false)]
                if (!pd.IsBrowsable)
                {
                    continue;
                }

                // Read-only properties
                if (!options.ShowReadOnlyProperties && pd.IsReadOnly())
                {
                    continue;
                }

                // If RequiredAttribute is set, skip properties that don't have the given attribute
                if (options.RequiredAttribute != null && pd.GetFirstAttributeOrDefault(options.RequiredAttribute) == null)
                {
                    continue;
                }

                yield return(this.CreatePropertyItem(pd, properties, instance));
            }
        }
        /// <summary>
        /// Creates property items for all properties in the specified object.
        /// </summary>
        /// <param name="instance">The object instance.</param>
        /// <param name="options">The options.</param>
        /// <returns>
        /// Enumeration of PropertyItem.
        /// </returns>
        protected virtual IEnumerable<PropertyItem> CreatePropertyItems(object instance, IPropertyGridOptions options)
        {
            var instanceType = instance.GetType();

            var properties = TypeDescriptor.GetProperties(instance);
            foreach (PropertyDescriptor pd in properties)
            {
                if (options.ShowDeclaredOnly && pd.ComponentType != instanceType)
                {
                    continue;
                }

                // Skip properties marked with [System.ComponentModel.Browsable(false)] or [PropertyTools.DataAnnotations.Browsable(false)]
                if (!pd.IsBrowsable())
                {
                    continue;
                }

                // Read-only properties
                if (!options.ShowReadOnlyProperties && pd.IsReadOnly())
                {
                    continue;
                }

                // If RequiredAttribute is set, skip properties that don't have the given attribute
                if (options.RequiredAttribute != null && pd.GetFirstAttributeOrDefault(options.RequiredAttribute) == null)
                {
                    continue;
                }

                yield return this.CreatePropertyItem(pd, properties, instance);
            }
        }
        /// <summary>
        /// Creates the property model.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="isEnumerable">if set to <c>true</c> [is enumerable].</param>
        /// <param name="options">The options.</param>
        /// <returns>
        /// A list of <see cref="Tab" /> .
        /// </returns>
        public virtual IEnumerable<Tab> CreateModel(object instance, bool isEnumerable, IPropertyGridOptions options)
        {
            if (instance == null)
            {
                return null;
            }

            this.Reset();

            var tabs = new Dictionary<string, Tab>();
            foreach (var pi in this.CreatePropertyItems(instance, options).OrderBy(t => t.SortIndex))
            {
                var tabHeader = pi.Tab ?? string.Empty;
                if (!tabs.ContainsKey(tabHeader))
                {
                    tabs.Add(tabHeader, new Tab { Header = pi.Tab });
                }

                var tab = tabs[tabHeader];
                var group = tab.Groups.FirstOrDefault(g => g.Header == pi.Category);
                if (group == null)
                {
                    group = new Group { Header = pi.Category };
                    tab.Groups.Add(group);
                }

                group.Properties.Add(pi);
            }

            return tabs.Values.ToList();
        }
        /// <summary>
        /// Creates property items for all properties in the specified object.
        /// </summary>
        /// <param name="instance">The object instance.</param>
        /// <param name="options">The options.</param>
        /// <returns>
        /// Enumeration of PropertyItem.
        /// </returns>
        protected virtual IEnumerable<PropertyItem> CreatePropertyItems(object instance, IPropertyGridOptions options)
        {
            var instanceType = instance.GetType();
            var metadataTypeAttribute = instanceType.GetCustomAttributes(typeof(MetadataTypeAttribute), true)
                                      .OfType<MetadataTypeAttribute>().FirstOrDefault();

            if (metadataTypeAttribute != null)
            {
                var metadataInstance = Activator.CreateInstance(metadataTypeAttribute.MetadataClassType);

                var instanceProperties = TypeDescriptor.GetProperties(instance);
                var metadataProperties = TypeDescriptor.GetProperties(metadataInstance);

                foreach (PropertyDescriptor mp in metadataProperties)
                {
                    var isPropertyOnInstance = false;

                    foreach (PropertyDescriptor pd in instanceProperties)
                    {
                        if (pd.Name == mp.Name)
                        {
                            isPropertyOnInstance = true;
                            break;
                        }
                    }

                    if (isPropertyOnInstance == false)
                        continue;

                    if (options.ShowDeclaredOnly && mp.ComponentType != instanceType)
                    {
                        continue;
                    }

                    // Skip properties marked with [System.ComponentModel.Browsable(false)] or [PropertyTools.DataAnnotations.Browsable(false)]
                    if (!mp.IsBrowsable())
                    {
                        continue;
                    }

                    // Read-only properties
                    if (!options.ShowReadOnlyProperties && mp.IsReadOnly())
                    {
                        continue;
                    }

                    // If RequiredAttribute is set, skip properties that don't have the given attribute
                    if (options.RequiredAttribute != null &&
                        mp.GetFirstAttributeOrDefault(options.RequiredAttribute) == null)
                    {
                        continue;
                    }

                    yield return this.CreatePropertyItem(mp, metadataProperties, instance);

                }
            }
            else
            {
                var properties = TypeDescriptor.GetProperties(instance);
                foreach (PropertyDescriptor pd in properties)
                {
                    if (options.ShowDeclaredOnly && pd.ComponentType != instanceType)
                    {
                        continue;
                    }

                    // Skip properties marked with [System.ComponentModel.Browsable(false)] or [PropertyTools.DataAnnotations.Browsable(false)]
                    if (!pd.IsBrowsable())
                    {
                        continue;
                    }

                    // Read-only properties
                    if (!options.ShowReadOnlyProperties && pd.IsReadOnly())
                    {
                        continue;
                    }

                    // If RequiredAttribute is set, skip properties that don't have the given attribute
                    if (options.RequiredAttribute != null &&
                        pd.GetFirstAttributeOrDefault(options.RequiredAttribute) == null)
                    {
                        continue;
                    }

                    yield return this.CreatePropertyItem(pd, properties, instance);
                }
            }
        }