0 out of 2 rated this helpful - Rate this topic

PropertyDescriptor Class

Provides an abstraction of a property on a class.

Namespace:  System.ComponentModel
Assembly:  System (in System.dll)
[ComVisibleAttribute(true)]
[HostProtectionAttribute(SecurityAction.LinkDemand, SharedState = true)]
public abstract class PropertyDescriptor : MemberDescriptor

The PropertyDescriptor type exposes the following members.

  Name Description
Protected method Supported by the XNA Framework PropertyDescriptor(MemberDescriptor) Initializes a new instance of the PropertyDescriptor class with the name and attributes in the specified MemberDescriptor.
Protected method Supported by the XNA Framework PropertyDescriptor(MemberDescriptor, Attribute[]) Initializes a new instance of the PropertyDescriptor class with the name in the specified MemberDescriptor and the attributes in both the MemberDescriptor and the Attribute array.
Protected method Supported by the XNA Framework PropertyDescriptor(String, Attribute[]) Initializes a new instance of the PropertyDescriptor class with the specified name and attributes.
Top
  Name Description
Protected property Supported by the XNA Framework AttributeArray Gets or sets an array of attributes. (Inherited from MemberDescriptor.)
Public property Attributes Gets the collection of attributes for this member. (Inherited from MemberDescriptor.)
Public property Category Gets the name of the category to which the member belongs, as specified in the CategoryAttribute. (Inherited from MemberDescriptor.)
Public property Supported by the XNA Framework ComponentType When overridden in a derived class, gets the type of the component this property is bound to.
Public property Converter Gets the type converter for this property.
Public property Description Gets the description of the member, as specified in the DescriptionAttribute. (Inherited from MemberDescriptor.)
Public property DesignTimeOnly Gets whether this member should be set only at design time, as specified in the DesignOnlyAttribute. (Inherited from MemberDescriptor.)
Public property Supported by the XNA Framework DisplayName Gets the name that can be displayed in a window, such as a Properties window. (Inherited from MemberDescriptor.)
Public property IsBrowsable Gets a value indicating whether the member is browsable, as specified in the BrowsableAttribute. (Inherited from MemberDescriptor.)
Public property IsLocalizable Gets a value indicating whether this property should be localized, as specified in the LocalizableAttribute.
Public property Supported by the XNA Framework IsReadOnly When overridden in a derived class, gets a value indicating whether this property is read-only.
Public property Supported by the XNA Framework Name Gets the name of the member. (Inherited from MemberDescriptor.)
Protected property Supported by the XNA Framework NameHashCode Gets the hash code for the name of the member, as specified in GetHashCode. (Inherited from MemberDescriptor.)
Public property Supported by the XNA Framework PropertyType When overridden in a derived class, gets the type of the property.
Public property SerializationVisibility Gets a value indicating whether this property should be serialized, as specified in the DesignerSerializationVisibilityAttribute.
Public property Supported by the XNA Framework SupportsChangeEvents Gets a value indicating whether value change notifications for this property may originate from outside the property descriptor.
Top
  Name Description
Public method Supported by the XNA Framework AddValueChanged Enables other objects to be notified when this property changes.
Public method Supported by the XNA Framework CanResetValue When overridden in a derived class, returns whether resetting an object changes its value.
Protected method Supported by the XNA Framework CreateAttributeCollection Creates a collection of attributes using the array of attributes passed to the constructor. (Inherited from MemberDescriptor.)
Protected method CreateInstance Creates an instance of the specified type.
Public method Supported by the XNA Framework Equals Compares this to another object to see if they are equivalent. (Overrides MemberDescriptor.Equals(Object).)

In XNA Framework 3.0, this member is inherited from Object.Equals(Object).
Protected method Supported by the XNA Framework FillAttributes Adds the attributes of the PropertyDescriptor to the specified list of attributes in the parent class. (Overrides MemberDescriptor.FillAttributes(IList).)

In XNA Framework 3.0, this member is inherited from MemberDescriptor.FillAttributes(IList).
Protected method Supported by the XNA Framework Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetChildProperties() Returns the default PropertyDescriptorCollection.
Public method GetChildProperties(Attribute[]) Returns a PropertyDescriptorCollection using a specified array of attributes as a filter.
Public method GetChildProperties(Object) Returns a PropertyDescriptorCollection for a given object.
Public method GetChildProperties(Object, Attribute[]) Returns a PropertyDescriptorCollection for a given object using a specified array of attributes as a filter.
Public method GetEditor Gets an editor of the specified type.
Public method Supported by the XNA Framework GetHashCode Returns the hash code for this object. (Overrides MemberDescriptor.GetHashCode().)

In XNA Framework, this member is overridden by GetHashCode().
Protected method GetInvocationTarget This method returns the object that should be used during invocation of members. (Overrides MemberDescriptor.GetInvocationTarget(Type, Object).)
Public method Supported by the XNA Framework GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method GetTypeFromName Returns a type using its name.
Public method Supported by the XNA Framework GetValue When overridden in a derived class, gets the current value of the property on a component.
Protected method Supported by the XNA Framework GetValueChangedHandler Retrieves the current set of ValueChanged event handlers for a specific component
Protected method Supported by the XNA Framework MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method Supported by the XNA Framework OnValueChanged Raises the ValueChanged event that you implemented.
Public method Supported by the XNA Framework RemoveValueChanged Enables other objects to be notified when this property changes.
Public method Supported by the XNA Framework ResetValue When overridden in a derived class, resets the value for this property of the component to the default value.
Public method Supported by the XNA Framework SetValue When overridden in a derived class, sets the value of the component to a different value.
Public method Supported by the XNA Framework ShouldSerializeValue When overridden in a derived class, determines a value indicating whether the value of this property needs to be persisted.
Public method Supported by the XNA Framework ToString Returns a string that represents the current object. (Inherited from Object.)
Top

A description of a property consists of a name, its attributes, the component class that the property is associated with, and the type of the property.

PropertyDescriptor provides the following properties and methods:

PropertyDescriptor also provides the following abstract properties and methods:

  • ComponentType contains the type of component this property is bound to.

  • IsReadOnly indicates whether this property is read-only.

  • PropertyType gets the type of the property.

  • CanResetValue indicates whether resetting the component changes the value of the component.

  • GetValue returns the current value of the property on a component.

  • ResetValue resets the value for this property of the component.

  • SetValue sets the value of the component to a different value.

  • ShouldSerializeValue indicates whether the value of this property needs to be persisted.

Typically, the abstract members are implemented through reflection. For more information about reflection, see the topics in Reflection.

Note Note

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: SharedState. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

The following code example is built upon the example in the PropertyDescriptorCollection class. It prints the information (category, description, display name) of the text of a button in a text box. It assumes that button1 and textbox1 have been instantiated on a form.


// Creates a new collection and assign it the properties for button1.
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(button1);

// Sets an PropertyDescriptor to the specific property.
System.ComponentModel.PropertyDescriptor myProperty = properties.Find("Text", false);

// Prints the property and the property description.
textBox1.Text = myProperty.DisplayName+ '\n' ;
textBox1.Text += myProperty.Description + '\n';
textBox1.Text += myProperty.Category + '\n';


The following code example shows how to implement a custom property descriptor that provides a read-only wrapper around a property. The SerializeReadOnlyPropertyDescriptor is used in a custom designer to provide a read-only property descriptor for the control's Size property.


using System;
using System.Collections;
using System.ComponentModel;
using System.Text;

namespace ReadOnlyPropertyDescriptorTest
{
    // The SerializeReadOnlyPropertyDescriptor shows how to implement a 
    // custom property descriptor. It provides a read-only wrapper 
    // around the specified PropertyDescriptor. 
    internal sealed class SerializeReadOnlyPropertyDescriptor : PropertyDescriptor
    {
        private PropertyDescriptor _pd = null;

        public SerializeReadOnlyPropertyDescriptor(PropertyDescriptor pd)
            : base(pd)
        {
            this._pd = pd;
        }

        public override AttributeCollection Attributes
        {
            get
            {
                return( AppendAttributeCollection(
                    this._pd.Attributes, 
                    ReadOnlyAttribute.Yes) );
            }
        }

        protected override void FillAttributes(IList attributeList)
        {
            attributeList.Add(ReadOnlyAttribute.Yes);
        }

        public override Type ComponentType
        {
            get
            {
                return this._pd.ComponentType;
            }
        }


        // The type converter for this property.
        // A translator can overwrite with its own converter.
        public override TypeConverter Converter
        {
            get
            {
                return this._pd.Converter;
            }
        }


        // Returns the property editor 
        // A translator can overwrite with its own editor.
        public override object GetEditor(Type editorBaseType)
        {
            return this._pd.GetEditor(editorBaseType);
        }

        // Specifies the property is read only.
        public override bool IsReadOnly
        {
            get
            {
                return true;
            }
        }

        public override Type PropertyType
        {
            get
            {
                return this._pd.PropertyType;
            }
        }

        public override bool CanResetValue(object component)
        {
            return this._pd.CanResetValue(component);
        }


        public override object GetValue(object component)
        {
            return this._pd.GetValue(component);
        }

        public override void ResetValue(object component)
        {
            this._pd.ResetValue(component);
        }

        public override void SetValue(object component, object val)
        {
            this._pd.SetValue(component, val);
        }

        // Determines whether a value should be serialized.
        public override bool ShouldSerializeValue(object component)
        {
            bool result = this._pd.ShouldSerializeValue(component);

            if (!result)
            {
                DefaultValueAttribute dva = (DefaultValueAttribute)_pd.Attributes[typeof(DefaultValueAttribute)];
                if (dva != null)
                {
                    result = !Object.Equals(this._pd.GetValue(component), dva.Value);
                }
                else
                {
                    result = true;
                }
            }

            return result;
        }

        // The following Utility methods create a new AttributeCollection
        // by appending the specified attributes to an existing collection.
        static public AttributeCollection AppendAttributeCollection(
            AttributeCollection existing, 
            params Attribute[] newAttrs)
        {
            return new AttributeCollection(AppendAttributes(existing, newAttrs));
        }


        static public Attribute[] AppendAttributes(
            AttributeCollection existing, 
            params Attribute[] newAttrs)
        {
            if (existing == null)
            {
                throw new ArgumentNullException("existing");
            }

            if (newAttrs == null)
            {
                newAttrs = new Attribute[0];
            }

            Attribute[] attributes;

            Attribute[] newArray = new Attribute[existing.Count + newAttrs.Length];
            int actualCount = existing.Count;
            existing.CopyTo(newArray, 0);

            for (int idx = 0; idx < newAttrs.Length; idx++)
            {
                if (newAttrs[idx] == null)
                {
                    throw new ArgumentNullException("newAttrs");
                }

                // Check if this attribute is already in the existing
                // array.  If it is, replace it.
                bool match = false;
                for (int existingIdx = 0; existingIdx < existing.Count; existingIdx++)
                {
                    if (newArray[existingIdx].TypeId.Equals(newAttrs[idx].TypeId))
                    {
                        match = true;
                        newArray[existingIdx] = newAttrs[idx];
                        break;
                    }
                }

                if (!match)
                {
                    newArray[actualCount++] = newAttrs[idx];
                }
            }

            // If some attributes were collapsed, create a new array.
            if (actualCount < newArray.Length)
            {
                attributes = new Attribute[actualCount];
                Array.Copy(newArray, 0, attributes, 0, actualCount);
            }
            else
            {
                attributes = newArray;
            }

            return attributes;
        }
    }
}


The following code examples show how to use the The SerializeReadOnlyPropertyDescriptor in a custom designer.


using System;
using System.Collections;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms.Design;

namespace ReadOnlyPropertyDescriptorTest
{   
    class DemoControlDesigner : ControlDesigner
    {
        // The PostFilterProperties method replaces the control's 
        // Size property with a read-only Size property by using 
        // the SerializeReadOnlyPropertyDescriptor class.
        protected override void PostFilterProperties(IDictionary properties)
        {
            if (properties.Contains("Size"))
            {
                PropertyDescriptor original = properties["Size"] as PropertyDescriptor;
                SerializeReadOnlyPropertyDescriptor readOnlyDescriptor = 
                    new SerializeReadOnlyPropertyDescriptor(original);

                properties["Size"] = readOnlyDescriptor;
            }

            base.PostFilterProperties(properties);
        }
    }
}


...


using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace ReadOnlyPropertyDescriptorTest
{
    [Designer(typeof(DemoControlDesigner))]
    public class DemoControl : Control
    {
        public DemoControl()
        {

        }
    }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ