PropertyDescriptor 類別

定義

提供類別上屬性的抽象。

public ref class PropertyDescriptor abstract : System::ComponentModel::MemberDescriptor
public abstract class PropertyDescriptor : System.ComponentModel.MemberDescriptor
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class PropertyDescriptor : System.ComponentModel.MemberDescriptor
type PropertyDescriptor = class
    inherit MemberDescriptor
[<System.Runtime.InteropServices.ComVisible(true)>]
type PropertyDescriptor = class
    inherit MemberDescriptor
Public MustInherit Class PropertyDescriptor
Inherits MemberDescriptor
繼承
PropertyDescriptor
衍生
屬性

範例

下列程式代碼範例是以 類別中的 PropertyDescriptorCollection 範例為基礎所建置。 它會列印文字框中按鈕文字 (類別、描述、顯示名稱) 的資訊。 它會假設 button1textbox1 已在表單上具現化。

// 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 = String::Concat( myProperty->DisplayName, "\n" );
textBox1->Text = String::Concat( textBox1->Text, myProperty->Description, "\n" );
textBox1->Text = String::Concat( textBox1->Text, myProperty->Category, "\n" );
// 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';
' 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

下列程式代碼範例示範如何實作自定義屬性描述元,以提供屬性的唯讀包裝函式。 SerializeReadOnlyPropertyDescriptor會用於自訂設計工具,以提供控件屬性的Size唯讀屬性描述元。

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(nameof(existing));
            }

            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;
        }
    }
}
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 - 1
                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

下列程式代碼範例示範如何在自訂設計工具中使用 SerializeReadOnlyPropertyDescriptor

using System.Collections;
using System.ComponentModel;
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);
        }
    }
}
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
using System.ComponentModel;
using System.Windows.Forms;

namespace ReadOnlyPropertyDescriptorTest
{
    [Designer(typeof(DemoControlDesigner))]
    public class DemoControl : Control
    {
        public DemoControl()
        {
        }
    }
}
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

備註

屬性的描述包含名稱、其屬性、屬性相關聯的元件類別,以及屬性的類型。

PropertyDescriptor 提供下列屬性和方法:

PropertyDescriptor 也提供下列 abstract 屬性和方法:

一般而言, abstract 成員是透過反映來實作。 如需反映的詳細資訊,請參閱 反映中的主題。

建構函式

PropertyDescriptor(MemberDescriptor)

使用指定 PropertyDescriptor 中的名稱和屬性,初始化 MemberDescriptor 類別的新執行個體。

PropertyDescriptor(MemberDescriptor, Attribute[])

使用指定 PropertyDescriptor 中的名稱,以及 MemberDescriptorMemberDescriptor 陣列中的屬性,初始化 Attribute 類別的新執行個體。

PropertyDescriptor(String, Attribute[])

使用指定的名稱和屬性,初始化 PropertyDescriptor 類別的新執行個體。

屬性

AttributeArray

取得或設定屬性的陣列。

(繼承來源 MemberDescriptor)
Attributes

取得這個成員的屬性集合。

(繼承來源 MemberDescriptor)
Category

取得成員所屬的分類名稱,如 CategoryAttribute 中所指定的。

(繼承來源 MemberDescriptor)
ComponentType

在衍生類別中覆寫時,取得這個屬性所繫結的元件型別。

Converter

取得這個屬性的型別轉換子。

Description

取得成員的描述,如 DescriptionAttribute 中所指定的。

(繼承來源 MemberDescriptor)
DesignTimeOnly

取得值,表示這個成員是否只在設計階段設定,如 DesignOnlyAttribute 中所指定的。

(繼承來源 MemberDescriptor)
DisplayName

取得可在視窗 (例如 [屬性] 視窗) 中顯示的名稱。

(繼承來源 MemberDescriptor)
IsBrowsable

取得值,表示成員是否可瀏覽,如 BrowsableAttribute 中指定的。

(繼承來源 MemberDescriptor)
IsLocalizable

取得值,表示這個屬性是否應該當地語系化,如 LocalizableAttribute 中指定的。

IsReadOnly

在衍生類別中覆寫時,取得數值以指示這個屬性是否為唯讀。

Name

取得成員的名稱。

(繼承來源 MemberDescriptor)
NameHashCode

取得成員名稱的雜湊程式碼,如 GetHashCode() 中所指定的。

(繼承來源 MemberDescriptor)
PropertyType

在衍生類別中覆寫時,取得屬性的型別。

SerializationVisibility

取得值,表示這個屬性是否應該序列化,如 DesignerSerializationVisibilityAttribute 中指定的。

SupportsChangeEvents

取得值,表示這個屬性的數值變更告知是否可能來自屬性描述項之外。

方法

AddValueChanged(Object, EventHandler)

使其他物件能在這個屬性變更時受到告知。

CanResetValue(Object)

在衍生類別中覆寫時,傳回重設物件是否變更其值。

CreateAttributeCollection()

使用傳遞給建構函式的屬性陣列,建立屬性的集合。

(繼承來源 MemberDescriptor)
CreateInstance(Type)

建立指定類型的執行個體。

Equals(Object)

比較這個與另一個物件,以檢視它們是否相等。

FillAttributes(IList)

PropertyDescriptor 的屬性加入至父類別中的指定屬性清單。

FillAttributes(IList)

當在衍生類別中覆寫時,將繼承類別的屬性加入父類別中屬性的指定清單。

(繼承來源 MemberDescriptor)
GetChildProperties()

傳回預設的 PropertyDescriptorCollection

GetChildProperties(Attribute[])

使用指定的屬性陣列做為篩選條件,傳回 PropertyDescriptorCollection

GetChildProperties(Object)

傳回指定物件的 PropertyDescriptorCollection

GetChildProperties(Object, Attribute[])

使用指定的屬性陣列做為篩選條件,傳回指定物件的 PropertyDescriptorCollection

GetEditor(Type)

取得指定型別的編輯器。

GetHashCode()

傳回此物件的雜湊碼。

GetInvocationTarget(Type, Object)

這個方法會傳回應該在成員的引動過程使用的物件。

GetInvocationTarget(Type, Object)

擷取應該在成員的引動過程使用的物件。

(繼承來源 MemberDescriptor)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
GetTypeFromName(String)

使用其名稱以傳回型別。

GetValue(Object)

在衍生類別中覆寫時,取得元件上屬性的目前值。

GetValueChangedHandler(Object)

擷取特定元件的目前事件處理程式集 ValueChanged

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
OnValueChanged(Object, EventArgs)

引發您實作 ValueChanged 的事件。

RemoveValueChanged(Object, EventHandler)

使其他物件能在這個屬性變更時受到告知。

ResetValue(Object)

在衍生類別中覆寫時,將元件的這個屬性值重設為預設值。

SetValue(Object, Object)

在衍生類別中覆寫時,將元件的值設定為不同的值。

ShouldSerializeValue(Object)

在衍生類別中覆寫時,判斷指示這個屬性的值是否需要保存的值。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱