Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

PropertyDescriptor Class

Provides an abstraction of a property on a class.

Namespace:  System.ComponentModel
Assembly:  System (in System.dll)

'Declaration
<ComVisibleAttribute(True)> _
<HostProtectionAttribute(SecurityAction.LinkDemand, SharedState := True)> _
Public MustInherit Class PropertyDescriptor _
	Inherits MemberDescriptor

The PropertyDescriptor type exposes the following members.

  NameDescription
Protected methodSupported by the XNA FrameworkPropertyDescriptor(MemberDescriptor)Initializes a new instance of the PropertyDescriptor class with the name and attributes in the specified MemberDescriptor.
Protected methodSupported by the XNA FrameworkPropertyDescriptor(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 methodSupported by the XNA FrameworkPropertyDescriptor(String, Attribute())Initializes a new instance of the PropertyDescriptor class with the specified name and attributes.
Top

  NameDescription
Protected propertySupported by the XNA FrameworkAttributeArrayGets or sets an array of attributes. (Inherited from MemberDescriptor.)
Public propertyAttributesGets the collection of attributes for this member. (Inherited from MemberDescriptor.)
Public propertyCategoryGets the name of the category to which the member belongs, as specified in the CategoryAttribute. (Inherited from MemberDescriptor.)
Public propertySupported by the XNA FrameworkComponentTypeWhen overridden in a derived class, gets the type of the component this property is bound to.
Public propertyConverterGets the type converter for this property.
Public propertyDescriptionGets the description of the member, as specified in the DescriptionAttribute. (Inherited from MemberDescriptor.)
Public propertyDesignTimeOnlyGets whether this member should be set only at design time, as specified in the DesignOnlyAttribute. (Inherited from MemberDescriptor.)
Public propertySupported by the XNA FrameworkDisplayNameGets the name that can be displayed in a window, such as a Properties window. (Inherited from MemberDescriptor.)
Public propertyIsBrowsableGets a value indicating whether the member is browsable, as specified in the BrowsableAttribute. (Inherited from MemberDescriptor.)
Public propertyIsLocalizableGets a value indicating whether this property should be localized, as specified in the LocalizableAttribute.
Public propertySupported by the XNA FrameworkIsReadOnlyWhen overridden in a derived class, gets a value indicating whether this property is read-only.
Public propertySupported by the XNA FrameworkNameGets the name of the member. (Inherited from MemberDescriptor.)
Protected propertySupported by the XNA FrameworkNameHashCodeGets the hash code for the name of the member, as specified in GetHashCode. (Inherited from MemberDescriptor.)
Public propertySupported by the XNA FrameworkPropertyTypeWhen overridden in a derived class, gets the type of the property.
Public propertySerializationVisibilityGets a value indicating whether this property should be serialized, as specified in the DesignerSerializationVisibilityAttribute.
Public propertySupported by the XNA FrameworkSupportsChangeEventsGets a value indicating whether value change notifications for this property may originate from outside the property descriptor.
Top

  NameDescription
Public methodSupported by the XNA FrameworkAddValueChangedEnables other objects to be notified when this property changes.
Public methodSupported by the XNA FrameworkCanResetValueWhen overridden in a derived class, returns whether resetting an object changes its value.
Protected methodSupported by the XNA FrameworkCreateAttributeCollectionCreates a collection of attributes using the array of attributes passed to the constructor. (Inherited from MemberDescriptor.)
Protected methodCreateInstanceCreates an instance of the specified type.
Public methodSupported by the XNA FrameworkEqualsCompares 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).


In .NET for Windows Phone apps Windows Phone 8.1, this member is inherited from Object.Equals(Object).
Protected methodSupported by the XNA FrameworkFillAttributesAdds 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 methodSupported by the XNA FrameworkFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetChildPropertiesReturns the default PropertyDescriptorCollection.
Public methodGetChildProperties(Attribute())Returns a PropertyDescriptorCollection using a specified array of attributes as a filter.
Public methodGetChildProperties(Object)Returns a PropertyDescriptorCollection for a given object.
Public methodGetChildProperties(Object, Attribute())Returns a PropertyDescriptorCollection for a given object using a specified array of attributes as a filter.
Public methodGetEditorGets an editor of the specified type.
Public methodSupported by the XNA FrameworkGetHashCodeReturns the hash code for this object. (Overrides MemberDescriptor.GetHashCode.)

In XNA Framework, this member is overridden by GetHashCode.
Protected methodGetInvocationTargetThis method returns the object that should be used during invocation of members. (Overrides MemberDescriptor.GetInvocationTarget(Type, Object).)
Public methodSupported by the XNA FrameworkGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodGetTypeFromNameReturns a type using its name.
Public methodSupported by the XNA FrameworkGetValueWhen overridden in a derived class, gets the current value of the property on a component.
Protected methodSupported by the XNA FrameworkGetValueChangedHandlerRetrieves the current set of ValueChanged event handlers for a specific component
Protected methodSupported by the XNA FrameworkMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkOnValueChangedRaises the ValueChanged event that you implemented.
Public methodSupported by the XNA FrameworkRemoveValueChangedEnables other objects to be notified when this property changes.
Public methodSupported by the XNA FrameworkResetValueWhen overridden in a derived class, resets the value for this property of the component to the default value.
Public methodSupported by the XNA FrameworkSetValueWhen overridden in a derived class, sets the value of the component to a different value.
Public methodSupported by the XNA FrameworkShouldSerializeValueWhen overridden in a derived class, determines a value indicating whether the value of this property needs to be persisted.
Public methodSupported by the XNA FrameworkToStringReturns 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 in the .NET Framework.

NoteNote

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. 
Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(Button1)

' Sets an PropertyDescriptor to the specific property. 
Dim myProperty As PropertyDescriptor = properties.Find("Text", False)

' Prints the property and the property description.
TextBox1.Text += myProperty.DisplayName & Microsoft.VisualBasic.ControlChars.Cr
TextBox1.Text += myProperty.Description & Microsoft.VisualBasic.ControlChars.Cr
TextBox1.Text += myProperty.Category & Microsoft.VisualBasic.ControlChars.Cr

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.

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Text

' The SerializeReadOnlyPropertyDescriptor shows how to implement a  
' custom property descriptor. It provides a read-only wrapper  
' around the specified PropertyDescriptor.  
Friend NotInheritable Class SerializeReadOnlyPropertyDescriptor
    Inherits PropertyDescriptor
    Private _pd As PropertyDescriptor = Nothing 


    Public Sub New(ByVal pd As PropertyDescriptor)
        MyBase.New(pd)
        Me._pd = pd

    End Sub 


    Public Overrides ReadOnly Property Attributes() As AttributeCollection
        Get 
            Return AppendAttributeCollection(Me._pd.Attributes, ReadOnlyAttribute.Yes)
        End Get 
    End Property 


    Protected Overrides Sub FillAttributes(ByVal attributeList As IList)
        attributeList.Add(ReadOnlyAttribute.Yes)

    End Sub 


    Public Overrides ReadOnly Property ComponentType() As Type
        Get 
            Return Me._pd.ComponentType
        End Get 
    End Property 


    ' The type converter for this property. 
    ' A translator can overwrite with its own converter. 
    Public Overrides ReadOnly Property Converter() As TypeConverter
        Get 
            Return Me._pd.Converter
        End Get 
    End Property 


    ' Returns the property editor  
    ' A translator can overwrite with its own editor. 
    Public Overrides Function GetEditor(ByVal editorBaseType As Type) As Object 
        Return Me._pd.GetEditor(editorBaseType)

    End Function 

    ' Specifies the property is read only. 
    Public Overrides ReadOnly Property IsReadOnly() As Boolean 
        Get 
            Return True 
        End Get 
    End Property 


    Public Overrides ReadOnly Property PropertyType() As Type
        Get 
            Return Me._pd.PropertyType
        End Get 
    End Property 


    Public Overrides Function CanResetValue(ByVal component As Object) As Boolean 
        Return Me._pd.CanResetValue(component)

    End Function 


    Public Overrides Function GetValue(ByVal component As Object) As Object 
        Return Me._pd.GetValue(component)

    End Function 


    Public Overrides Sub ResetValue(ByVal component As Object)
        Me._pd.ResetValue(component)

    End Sub 


    Public Overrides Sub SetValue(ByVal component As Object, ByVal val As Object)
        Me._pd.SetValue(component, val)

    End Sub 

    ' Determines whether a value should be serialized. 
    Public Overrides Function ShouldSerializeValue(ByVal component As Object) As Boolean 
        Dim result As Boolean = Me._pd.ShouldSerializeValue(component)

        If Not result Then 
            Dim dva As DefaultValueAttribute = _
                CType(_pd.Attributes(GetType(DefaultValueAttribute)), DefaultValueAttribute)
            If Not (dva Is Nothing) Then
                result = Not [Object].Equals(Me._pd.GetValue(component), dva.Value)
            Else
                result = True 
            End If 
        End If 

        Return result

    End Function 


    ' The following Utility methods create a new AttributeCollection 
    ' by appending the specified attributes to an existing collection. 
    Public Shared Function AppendAttributeCollection( _
        ByVal existing As AttributeCollection, _
        ByVal ParamArray newAttrs() As Attribute) As AttributeCollection

        Return New AttributeCollection(AppendAttributes(existing, newAttrs))

    End Function 

    Public Shared Function AppendAttributes( _
        ByVal existing As AttributeCollection, _
        ByVal ParamArray newAttrs() As Attribute) As Attribute()

        If existing Is Nothing Then 
            Throw New ArgumentNullException("existing")
        End If 

        If newAttrs Is Nothing Then
            newAttrs = New Attribute(-1) {}
        End If 

        Dim attributes() As Attribute

        Dim newArray(existing.Count + newAttrs.Length) As Attribute
        Dim actualCount As Integer = existing.Count
        existing.CopyTo(newArray, 0)

        Dim idx As Integer 
        For idx = 0 To newAttrs.Length
            If newAttrs(idx) Is Nothing Then 
                Throw New ArgumentNullException("newAttrs")
            End If 

            ' Check if this attribute is already in the existing 
            ' array.  If it is, replace it. 
            Dim match As Boolean = False 
            Dim existingIdx As Integer 
            For existingIdx = 0 To existing.Count
                If newArray(existingIdx).TypeId.Equals(newAttrs(idx).TypeId) Then
                    match = True
                    newArray(existingIdx) = newAttrs(idx)
                    Exit For 
                End If 
            Next existingIdx

            If Not match Then
                actualCount += 1
                newArray(actualCount) = newAttrs(idx)
            End If 
        Next idx

        ' If some attributes were collapsed, create a new array. 
        If actualCount < newArray.Length Then
            attributes = New Attribute(actualCount) {}
            Array.Copy(newArray, 0, attributes, 0, actualCount)
        Else
            attributes = newArray
        End If 

        Return attributes

    End Function 
End Class

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

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Text
Imports System.Windows.Forms.Design

Class DemoControlDesigner
    Inherits ControlDesigner

    ' The PostFilterProperties method replaces the control's  
    ' Size property with a read-only Size property by using  
    ' the SerializeReadOnlyPropertyDescriptor class. 
    Protected Overrides Sub PostFilterProperties(ByVal properties As IDictionary) 
        If properties.Contains("Size") Then 
            Dim original As PropertyDescriptor = properties("Size")

            Dim readOnlyDescriptor As New SerializeReadOnlyPropertyDescriptor(original)

            properties("Size") = readOnlyDescriptor
        End If 

        MyBase.PostFilterProperties(properties)

    End Sub 
End Class


...


Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Text
Imports System.Windows.Forms
Imports System.Windows.Forms.Design


<Designer(GetType(DemoControlDesigner))>  _
Public Class DemoControl
    Inherits Control

    Public Sub New() 

    End Sub 
End Class

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2015 Microsoft