protected virtual void Describe(PropertyGridProperty property, PropertyDescriptor descriptor)
        {
            if (property == null)
            {
                throw new ArgumentNullException("property");
            }

            if (descriptor == null)
            {
                throw new ArgumentNullException("descriptor");
            }

            property.Descriptor   = descriptor;
            property.Name         = descriptor.Name;
            property.PropertyType = descriptor.PropertyType;

            // unset by default. conversion service does the default job
            //property.Converter = descriptor.Converter;

            property.Category    = string.IsNullOrWhiteSpace(descriptor.Category) || descriptor.Category.EqualsIgnoreCase(CategoryAttribute.Default.Category) ? Grid.DefaultCategoryName : descriptor.Category;
            property.IsReadOnly  = descriptor.IsReadOnly;
            property.Description = descriptor.Description;
            property.DisplayName = descriptor.DisplayName;
            if (property.DisplayName == descriptor.Name)
            {
                property.DisplayName = DecamelizationService.Decamelize(property.DisplayName);
            }

            property.IsEnum      = descriptor.PropertyType.IsEnum;
            property.IsFlagsEnum = descriptor.PropertyType.IsEnum && Extensions.IsFlagsEnum(descriptor.PropertyType);

            DefaultValueAttribute att = descriptor.GetAttribute <DefaultValueAttribute>();

            property.HasDefaultValue = att != null;
            property.DefaultValue    = att != null ? att.Value : null;

            PropertyGridOptionsAttribute options = descriptor.GetAttribute <PropertyGridOptionsAttribute>();

            if (options != null)
            {
                if (options.SortOrder != 0)
                {
                    property.SortOrder = options.SortOrder;
                }

                property.IsEnum      = options.IsEnum;
                property.IsFlagsEnum = options.IsFlagsEnum;
            }

            AddDynamicProperties(descriptor.Attributes.OfType <PropertyGridAttribute>(), property.Attributes);
            AddDynamicProperties(descriptor.PropertyType.GetAttributes <PropertyGridAttribute>(), property.TypeAttributes);
        }
示例#2
0
        private CmdLineProperty GetCmdLineProperty(PropertyDescriptor propDesc, T obj)
        {
            var prop      = new CmdLineProperty(propDesc, obj);
            var claAtt    = propDesc.GetAttribute <CmdLineArgAttribute>();
            var showUsage = (bool?)null;

            if (claAtt != null)
            {
                prop.Aliases          = claAtt.Aliases;
                showUsage             = claAtt.mShowInUsage;     // Use internal field instead of property so we can get null value.
                prop.ShowInHelp       = claAtt.ShowInHelp;
                prop.ShowDefaultValue = claAtt.ShowDefaultValue;
            }

            prop.Required = propDesc.GetAttribute <RequiredAttribute>() != null;

            // Determine if we should show this in the usage or not.
            if (showUsage ?? false)
            {
                prop.ShowInUsage = true;
            }
            else if (showUsage ?? prop.Required)
            {
                prop.ShowInUsage = true;
            }
            else
            {
                prop.ShowInUsage = false;
            }

            prop.DefaultValue = prop.Value;

            // Make sure aliases has a valid value so we don't have to check for nulls everywhere.
            if (prop.Aliases == null)
            {
                prop.Aliases = new string[] { }
            }
            ;

            return(prop);
        }
        public virtual PropertyGridProperty CreateProperty(PropertyDescriptor descriptor)
        {
            if (descriptor == null)
            {
                throw new ArgumentNullException("descriptor");
            }

            bool forceReadWrite = false;
            PropertyGridProperty         property = null;
            PropertyGridOptionsAttribute options  = descriptor.GetAttribute <PropertyGridOptionsAttribute>();

            if (options != null)
            {
                forceReadWrite = options.ForceReadWrite;
                if (options.PropertyType != null)
                {
                    property = (PropertyGridProperty)Activator.CreateInstance(options.PropertyType, this);
                }
            }

            if (property == null)
            {
                options = descriptor.PropertyType.GetAttribute <PropertyGridOptionsAttribute>();
                if (options != null)
                {
                    if (!forceReadWrite)
                    {
                        forceReadWrite = options.ForceReadWrite;
                    }
                    if (options.PropertyType != null)
                    {
                        property = (PropertyGridProperty)Activator.CreateInstance(options.PropertyType, this);
                    }
                }
            }

            if (property == null)
            {
                property = CreateProperty();
            }

            Describe(property, descriptor);
            if (forceReadWrite)
            {
                property.IsReadOnly = false;
            }
            property.OnDescribed();
            property.RefreshValueFromDescriptor();
            return(property);
        }
        private NewVariableAttribute GetNewVariableDefaults(PropertyDescriptor property, object target)
        {
            var defaultAttr = property.GetAttribute <NewVariableAttribute>();

            if (defaultAttr != null)
            {
                return(defaultAttr);
            }

            //Look for the interface
            var ifc = target as INewVariableProvider;

            if (ifc != null)
            {
                return(ifc.GetNewVariable(property.Name));
            }

            return(new NewVariableAttribute(property.Name, typeof(int)));
        }
示例#5
0
            private void WriteProperty(XContainer parentElement, object obj, string name, PropertyDescriptor prop)
            {
                SerializerOptionsAttribute att = prop != null?prop.GetAttribute <SerializerOptionsAttribute>() ?? new SerializerOptionsAttribute() : new SerializerOptionsAttribute();

                if (!string.IsNullOrEmpty(att.Redirect))
                {
                    string newName = name;

                    var namable = obj as INamable;
                    if (namable != null)
                    {
                        newName = namable.Name;
                    }

                    parentElement.Add(new XElement(name, att.Redirect + "/" + newName));
                    this.WriteRedirect(att.Redirect, obj, newName);
                }
                else
                {
                    this.AddObjectToXmlDoc(parentElement, obj, name);
                }
            }
        /// <summary>
        /// Does the wrapped <see cref="PropertyInfo"/> have an attribute of type <typeparamref name="T"/>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static bool HasAttribute <T>(this PropertyDescriptor prop) where T : Attribute
        {
            T attribute = prop.GetAttribute <T>();

            return(attribute != null);
        }
            private static IList <PropertyDescriptor[]> GetCommonProperties(
                object[] objects,
                bool presort,
                PropertyTab tab,
                GridEntry parentEntry)
            {
                var objectProperties = new PropertyDescriptorCollection[objects.Length];
                var attributes       = new Attribute[parentEntry.BrowsableAttributes.Count];

                parentEntry.BrowsableAttributes.CopyTo(attributes, 0);

                for (int i = 0; i < objects.Length; i++)
                {
                    var properties = tab.GetProperties(parentEntry, objects[i], attributes);
                    if (presort)
                    {
                        properties = properties.Sort(s_propertyComparer);
                    }

                    objectProperties[i] = properties;
                }

                List <PropertyDescriptor[]> mergedList = new();
                var matchArray = new PropertyDescriptor[objects.Length];

                //
                // Merge the property descriptors
                //

                int[] positions = new int[objectProperties.Length];

                // Iterate through the first object's properties to see if it has matches in the other objects.
                for (int i = 0; i < objectProperties[0].Count; i++)
                {
                    PropertyDescriptor pivotProperty = objectProperties[0][i];

                    bool match = pivotProperty.GetAttribute <MergablePropertyAttribute>().IsDefaultAttribute();

                    for (int j = 1; match && j < objectProperties.Length; j++)
                    {
                        if (positions[j] >= objectProperties[j].Count)
                        {
                            match = false;
                            break;
                        }

                        // Check to see if we're on a match.
                        PropertyDescriptor property = objectProperties[j][positions[j]];
                        if (pivotProperty.Equals(property))
                        {
                            positions[j] += 1;

                            if (!property.GetAttribute <MergablePropertyAttribute>().IsDefaultAttribute())
                            {
                                match = false;
                                break;
                            }

                            matchArray[j] = property;
                            continue;
                        }

                        int position = positions[j];
                        property = objectProperties[j][position];

                        match = false;

                        // If we aren't on a match, check all the items until we're past where the matching item would be.
                        while (s_propertyComparer.Compare(property, pivotProperty) <= 0)
                        {
                            // Got a match!
                            if (pivotProperty.Equals(property))
                            {
                                if (!property.GetAttribute <MergablePropertyAttribute>().IsDefaultAttribute())
                                {
                                    match = false;
                                    position++;
                                }
                                else
                                {
                                    match         = true;
                                    matchArray[j] = property;
                                    positions[j]  = position + 1;
                                }

                                break;
                            }

                            // Try again.
                            position++;
                            if (position < objectProperties[j].Count)
                            {
                                property = objectProperties[j][position];
                            }
                            else
                            {
                                break;
                            }
                        }

                        // If we got here, there is no match, quit for this one.
                        if (!match)
                        {
                            positions[j] = position;
                            break;
                        }
                    }

                    // Do we have a match?
                    if (match)
                    {
                        matchArray[0] = pivotProperty;
                        mergedList.Add((PropertyDescriptor[])matchArray.Clone());
                    }
                }

                return(mergedList);
            }