public void Ctor_DesignOnly(bool designOnly)
        {
            var attribute = new DesignOnlyAttribute(designOnly);

            Assert.Equal(designOnly, attribute.IsDesignOnly);
            Assert.Equal(!designOnly, attribute.IsDefaultAttribute());
        }
 public void Equals_Object_ReturnsExpected(DesignOnlyAttribute attribute, object other, bool expected)
 {
     Assert.Equal(expected, attribute.Equals(other));
     if (other is DesignOnlyAttribute otherAttribute)
     {
         Assert.Equal(expected, attribute.GetHashCode().Equals(other.GetHashCode()));
     }
 }
        public override bool Equals(object obj)
        {
            if (obj == this)
            {
                return(true);
            }

            DesignOnlyAttribute other = obj as DesignOnlyAttribute;

            return((other != null) && other.IsDesignOnly == IsDesignOnly);
        }
示例#4
0
        // Determine if two objects are equal.
        public override bool Equals(Object obj)
        {
            DesignOnlyAttribute other = (obj as DesignOnlyAttribute);

            if (other != null)
            {
                return(other.designOnly == designOnly);
            }
            else
            {
                return(false);
            }
        }
示例#5
0
        /// <include file='doc\AdvancedBindingObject.uex' path='docs/doc[@for="AdvancedBindingObject.ICustomTypeDescriptor.GetProperties1"]/*' />
        /// <devdoc>
        ///     Retrieves an array of properties that the given component instance
        ///     provides.  This may differ from the set of properties the class
        ///     provides.  If the component is sited, the site may add or remove
        ///     additional properties.  The returned array of properties will be
        ///     filtered by the given set of attributes.
        /// </devdoc>
        PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
        {
            if (propsCollection == null)
            {
                Control control = bindings.Control;
                Type    type    = control.GetType();

                PropertyDescriptorCollection bindableProperties = TypeDescriptor.GetProperties(control, attributes);

                AttributeCollection      controlAttributes = TypeDescriptor.GetAttributes(type);
                DefaultPropertyAttribute defPropAttr       = ((DefaultPropertyAttribute)controlAttributes[typeof(DefaultPropertyAttribute)]);

                ArrayList props = new ArrayList();
                for (int i = 0; i < bindableProperties.Count; i++)
                {
                    if (bindableProperties[i].IsReadOnly)
                    {
                        continue;
                    }
                    bool bindable = ((BindableAttribute)bindableProperties[i].Attributes[typeof(BindableAttribute)]).Bindable;
                    DesignOnlyAttribute             designAttr = ((DesignOnlyAttribute)bindableProperties[i].Attributes[typeof(DesignOnlyAttribute)]);
                    DesignBindingPropertyDescriptor property   = new DesignBindingPropertyDescriptor(bindableProperties[i], null);
                    property.AddValueChanged(bindings, new EventHandler(OnBindingChanged));
                    // ASURT 45429: skip the Design time properties
                    //
                    if (!bindable && !showAll || designAttr.IsDesignOnly)
                    {
                        if (((DesignBinding)property.GetValue(this)).IsNull)
                        {
                            continue;
                        }
                    }
                    props.Add(property);

                    // ASURT 45429: make the default property have focus in the properties window
                    //
                    if (defPropAttr != null && property.Name.Equals(defPropAttr.Name))
                    {
                        System.Diagnostics.Debug.Assert(this.defaultProp == null, "a control cannot have two default properties");
                        this.defaultProp = property;
                    }
                }
                PropertyDescriptor[] propArray = new PropertyDescriptor[props.Count];
                props.CopyTo(propArray, 0);
                this.propsCollection = new PropertyDescriptorCollection(propArray);
            }
            return(propsCollection);
        }
 public void DefaultProperties_GetDesignOnly_ReturnsExpected(DesignOnlyAttribute attribute, bool expectedDesignOnly)
 {
     Assert.Equal(expectedDesignOnly, attribute.IsDesignOnly);
     Assert.Equal(!expectedDesignOnly, attribute.IsDefaultAttribute());
 }
示例#7
0
        /// <summary>
        ///    <para>
        ///       Persists the properties of a tag.
        ///    </para>
        /// </summary>
        /// <param name='sw'>
        ///    The string writer to use.
        /// </param>
        /// <param name=' component'>
        ///    The component to persist.
        /// </param>
        /// <param name=' prefix'>
        ///    The prefix to store.
        /// </param>
        /// <param name=' propDesc'>
        ///    A property descriptor for the tag properties.
        /// </param>
        private static void PersistAttributes(TextWriter sw, object component, string prefix, PropertyDescriptor propDesc)
        {
            PropertyDescriptorCollection properties;
            string persistPrefix = String.Empty;
            object value         = component;

            if (propDesc != null)
            {
                value      = propDesc.GetValue(component);
                properties = TypeDescriptor.GetProperties(propDesc.PropertyType,
                                                          new Attribute[] {
                    PersistenceModeAttribute.Attribute
                });
            }
            else
            {
                properties = TypeDescriptor.GetProperties(component,
                                                          new Attribute[] {
                    PersistenceModeAttribute.Attribute
                });
            }

            if (value == null)
            {
                return;
            }

            if (prefix.Length != 0)
            {
                persistPrefix = prefix + "-";
            }

            DataBindingCollection dataBindings = null;
            bool isControl = (component is Control);

            if ((component is IDataBindingsAccessor))
            {
                dataBindings = ((IDataBindingsAccessor)component).DataBindings;
            }

            if (component is DeviceSpecificChoice)
            {
                properties = properties.Sort(new String[] { "Filter" });
            }

            for (int i = 0; i < properties.Count; i++)
            {
                // Skip properties that are hidden to the serializer
                if (properties[i].SerializationVisibility == DesignerSerializationVisibility.Hidden)
                {
                    continue;
                }

                // Skip design-time only properties such as DefaultModifiers and Name
                DesignOnlyAttribute doAttr = (DesignOnlyAttribute)properties[i].Attributes[typeof(DesignOnlyAttribute)];
                if ((doAttr != null) && doAttr.IsDesignOnly)
                {
                    continue;
                }

                string propName = properties[i].Name;
                Type   propType = properties[i].PropertyType;

                object obj = properties[i].GetValue(value);
                if (obj == null)
                {
                    continue;
                }

                DefaultValueAttribute defValAttr =
                    (DefaultValueAttribute)properties[i].Attributes[typeof(DefaultValueAttribute)];
                if ((defValAttr != null) && (obj.Equals(defValAttr.Value)))
                {
                    continue;
                }

                string persistName = propName;

/* AUI Change 3876 -- Change is taken out because of 4347
 *              if (component is DeviceSpecificChoice && persistName.Equals("Xmlns"))
 *              {
 *                  persistName = "xmlns";
 *              }
 * End of Change */

                if (prefix.Length != 0)
                {
                    persistName = persistPrefix + persistName;
                }

                PropertyDescriptorCollection subProps = null;
                if (properties[i].SerializationVisibility == DesignerSerializationVisibility.Content)
                {
                    subProps = TypeDescriptor.GetProperties(propType);
                }
                if ((subProps == null) || (subProps.Count == 0))
                {
                    string persistValue = null;

                    // TODO: Use consts or have DataBinding store both OM name and persist name
                    DataBinding db = null;
                    if (dataBindings != null)
                    {
                        db = dataBindings[persistName.Replace('.', '-')];
                    }

                    if (db == null)
                    {
                        if (propType.IsEnum)
                        {
                            persistValue = Enum.Format(propType, obj, "G");
                        }
                        else if (propType == typeof(string))
                        {
                            persistValue = HttpUtility.HtmlEncode(obj.ToString());
                        }
                        else
                        {
                            TypeConverter converter = properties[i].Converter;
                            if (converter != null)
                            {
                                persistValue = converter.ConvertToInvariantString(null, obj);
                            }
                            else
                            {
                                persistValue = obj.ToString();
                            }
                            persistValue = HttpUtility.HtmlEncode(persistValue);
                        }

                        if ((persistValue == null) ||
                            (persistValue.Equals("NotSet")) ||
                            (propType.IsArray && (persistValue.Length == 0)))
                        {
                            continue;
                        }

                        sw.Write(" ");
                        sw.Write(persistName);
                        sw.Write("=\"");

                        sw.Write(persistValue);
                        sw.Write("\"");
                    }
                }
                else
                {
/*
 *  This will force all ListDictionary properties with DesignerSerializationVisibility.Content atttribute be
 *  persisted as a series of attributes eg. <PropertyFoo Key1="Value1" Key2="Value2" ... />. The
 *  WebControlPersistor is not able to handle this case and will return undesired results.
 */
// AUI Change to handle DeviceSpecificChoice.Contents
                    if (obj is ListDictionary)
                    {
                        IDictionaryEnumerator enumerator = ((ListDictionary)obj).GetEnumerator();
                        String persistValue = null;

                        while (enumerator.MoveNext())
                        {
                            propName     = enumerator.Key as String;
                            persistValue = enumerator.Value as String;

                            Debug.Assert(propName != null,
                                         "Non-string key in DeviceSpecificChoice Contents.");

                            if ((propName.Length == 0) ||
                                (persistValue == null))
                            {
                                continue;
                            }

                            sw.Write(" ");
                            sw.Write(propName);
                            sw.Write("=\"");

                            HttpUtility.HtmlEncode((String)persistValue, sw);
                            sw.Write("\"");
                        }
                    }
                    else
                    {
// End of AUI Change

                        // there are sub properties, don't persist this object, but
                        // recursively persist the subproperties.
                        PersistAttributes(sw, obj, persistName, null);
                    }
                }
            }

            // Persist all the databindings on this control
            if (isControl)
            {
                PersistDataBindings(sw, (Control)component);

                AttributeCollection expandos = null;
                if (component is WebControl)
                {
                    expandos = ((WebControl)component).Attributes;
                }
                else if (component is HtmlControl)
                {
                    expandos = ((HtmlControl)component).Attributes;
                }
                else if (component is UserControl)
                {
                    expandos = ((UserControl)component).Attributes;
                }

                if (expandos != null)
                {
                    foreach (string key in expandos.Keys)
                    {
                        sw.Write(" ");
                        sw.Write(key);
                        sw.Write("=\"");
                        sw.Write(expandos[key]);
                        sw.Write("\"");
                    }
                }
            }
        }
        /// <include file='doc\WebControlPersister.uex' path='docs/doc[@for="ControlPersister.PersistAttributes"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Persists the properties of a tag.
        ///    </para>
        /// </devdoc>
        private static void PersistAttributes(TextWriter sw, object component, string prefix, PropertyDescriptor propDesc)
        {
            PropertyDescriptorCollection properties;
            string persistPrefix = String.Empty;
            object value         = component;

            if (propDesc != null)
            {
                value      = propDesc.GetValue(component);
                properties = TypeDescriptor.GetProperties(propDesc.PropertyType,
                                                          new Attribute[] {
                    PersistenceModeAttribute.Attribute
                });
            }
            else
            {
                properties = TypeDescriptor.GetProperties(component,
                                                          new Attribute[] {
                    PersistenceModeAttribute.Attribute
                });
            }

            if (value == null)
            {
                return;
            }

            if (prefix.Length != 0)
            {
                persistPrefix = prefix + "-";
            }

            DataBindingCollection dataBindings = null;
            bool isControl = (component is Control);

            if ((component is IDataBindingsAccessor))
            {
                dataBindings = ((IDataBindingsAccessor)component).DataBindings;
            }

            for (int i = 0; i < properties.Count; i++)
            {
                // Skip properties that are hidden to the serializer
                if (properties[i].SerializationVisibility == DesignerSerializationVisibility.Hidden)
                {
                    continue;
                }

                // Skip design-time only properties such as DefaultModifiers and Name
                DesignOnlyAttribute doAttr = (DesignOnlyAttribute)properties[i].Attributes[typeof(DesignOnlyAttribute)];
                if ((doAttr != null) && doAttr.IsDesignOnly)
                {
                    continue;
                }

                string propName = properties[i].Name;
                Type   propType = properties[i].PropertyType;

                object obj = properties[i].GetValue(value);
                if (obj == null)
                {
                    continue;
                }

                DefaultValueAttribute defValAttr =
                    (DefaultValueAttribute)properties[i].Attributes[typeof(DefaultValueAttribute)];
                if ((defValAttr != null) && (obj.Equals(defValAttr.Value)))
                {
                    continue;
                }

                string persistName = propName;
                if (prefix.Length != 0)
                {
                    persistName = persistPrefix + persistName;
                }

                PropertyDescriptorCollection subProps = null;
                if (properties[i].SerializationVisibility == DesignerSerializationVisibility.Content)
                {
                    subProps = TypeDescriptor.GetProperties(propType);
                }
                if ((subProps == null) || (subProps.Count == 0))
                {
                    string persistValue = null;

                    // TODO: Use consts or have DataBinding store both OM name and persist name
                    DataBinding db = null;
                    if (dataBindings != null)
                    {
                        db = dataBindings[persistName.Replace('.', '-')];
                    }

                    if (db == null)
                    {
                        if (propType.IsEnum)
                        {
                            persistValue = Enum.Format(propType, obj, "G");
                        }
                        else if (propType == typeof(string))
                        {
                            persistValue = HttpUtility.HtmlEncode(obj.ToString());
                        }
                        else
                        {
                            TypeConverter converter = properties[i].Converter;
                            if (converter != null)
                            {
                                persistValue = converter.ConvertToInvariantString(null, obj);
                            }
                            else
                            {
                                persistValue = obj.ToString();
                            }
                            persistValue = HttpUtility.HtmlEncode(persistValue);
                        }

                        if ((persistValue == null) ||
                            (persistValue.Equals("NotSet")) ||
                            (propType.IsArray && (persistValue.Length == 0)))
                        {
                            continue;
                        }

                        sw.Write(" ");
                        sw.Write(persistName);
                        sw.Write("=\"");

                        sw.Write(persistValue);
                        sw.Write("\"");
                    }
                }
                else
                {
                    // there are sub properties, don't persist this object, but
                    // recursively persist the subproperties.
                    PersistAttributes(sw, obj, persistName, null);
                }
            }

            // Persist all the databindings on this control
            if (isControl)
            {
                PersistDataBindings(sw, (Control)component);

                AttributeCollection expandos = null;
                if (component is WebControl)
                {
                    expandos = ((WebControl)component).Attributes;
                }
                else if (component is HtmlControl)
                {
                    expandos = ((HtmlControl)component).Attributes;
                }
                else if (component is UserControl)
                {
                    expandos = ((UserControl)component).Attributes;
                }

                if (expandos != null)
                {
                    foreach (string key in expandos.Keys)
                    {
                        sw.Write(" ");
                        sw.Write(key);
                        sw.Write("=\"");
                        sw.Write(expandos[key]);
                        sw.Write("\"");
                    }
                }
            }
        }
示例#9
0
        public void GetIsDesignOnly(bool isDesignOnly)
        {
            var attribute = new DesignOnlyAttribute(isDesignOnly);

            Assert.Equal(isDesignOnly, attribute.IsDesignOnly);
        }
        /// <include file='doc\UserControlDesigner.uex' path='docs/doc[@for="ControlDesigner.PersistProperties"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Enumerates and persists all properties of a control
        ///       including sub properties.
        ///    </para>
        /// </devdoc>
        private string PersistProperties(string strPrefix, object parent, PropertyDescriptor propDesc)
        {
            try {
                // Obtain the HTML element to which the associated behavior is attached to.
                Debug.Assert(parent != null, "parent is null");

                StringBuilder sb       = new StringBuilder(strPrefix); // property name of the parent property
                object        objValue = null;

                PropertyDescriptorCollection properties;

                // get all sub properties for parent property
                if (propDesc == null)
                {
                    objValue   = parent;
                    properties = TypeDescriptor.GetProperties(parent,
                                                              new Attribute[] {
                        PersistenceModeAttribute.Attribute
                    });
                }
                else
                {
                    objValue = propDesc.GetValue(parent); // value object for parent property
                    if (propDesc.SerializationVisibility != DesignerSerializationVisibility.Content)
                    {
                        return(sb.ToString());
                    }

                    properties = TypeDescriptor.GetProperties(propDesc.PropertyType,
                                                              new Attribute[] {
                        PersistenceModeAttribute.Attribute
                    });
                }

                for (int i = 0; i < properties.Count; i++)
                {
                    // Only deal with attributes that need to be persisted
                    if (properties[i].SerializationVisibility == DesignerSerializationVisibility.Hidden)
                    {
                        continue;
                    }

                    // Skip design-time only properties such as DefaultModifiers
                    DesignOnlyAttribute doAttr = (DesignOnlyAttribute)properties[i].Attributes[typeof(DesignOnlyAttribute)];
                    if ((doAttr != null) && doAttr.IsDesignOnly)
                    {
                        continue;
                    }

                    // We don't want to persist these designer properties on a control
                    if (parent is WebUIControl)
                    {
                        if (properties[i].Name.Equals("ID"))
                        {
                            continue;
                        }
                    }

                    // The "objNewValue" is being moved to PersistAttributes(), but we leave
                    // this variable here in order to test if this is a System.Strings[] and
                    // break out of the for loop (which cannot be done from inside
                    // PersistAttributes()

                    object objNewValue = properties[i].GetValue(objValue);

                    if (sb.Length != 0)
                    {
                        sb.Append("-");
                    }

                    sb.Append(properties[i].Name);
                    sb = new StringBuilder(PersistProperties(sb.ToString(), objValue, properties[i]));

                    // check if this property has subproperties, if not, persist it as an attribute. Need to check
                    // for NotifyParentPropertyAttribute to ensure it's really a bottom property
                    PropertyDescriptorCollection array = TypeDescriptor.GetProperties(properties[i].PropertyType,
                                                                                      new Attribute[] {
                        PersistenceModeAttribute.Attribute,
                        NotifyParentPropertyAttribute.Yes
                    });
                    if (array == null || array.Count == 0)
                    {
                        try {
                            PersistAttribute(properties[i], objValue, sb.ToString());
                        }
                        catch (Exception) {
                            //  Debug.Fail(e.ToString());
                        }
                    }

                    sb = new StringBuilder(strPrefix);
                }

                return(sb.ToString());
            }
            catch (Exception e) {
                Debug.Fail(e.ToString());
                return(string.Empty);
            }
        }