PropertyDescriptor Kelas

Definisi

Menyediakan abstraksi properti pada kelas.

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
Warisan
PropertyDescriptor
Turunan
Atribut

Contoh

Contoh kode berikut dibangun berdasarkan contoh di PropertyDescriptorCollection kelas . Ini mencetak informasi (kategori, deskripsi, nama tampilan) teks tombol dalam kotak teks. Ini mengasumsikan bahwa button1 dan textbox1 telah diinstansiasi pada formulir.

// 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

Contoh kode berikut menunjukkan cara mengimplementasikan deskriptor properti kustom yang menyediakan pembungkus baca-saja di sekitar properti. SerializeReadOnlyPropertyDescriptor digunakan dalam perancang kustom untuk menyediakan deskriptor properti baca-saja untuk properti kontrolSize.

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

Contoh kode berikut menunjukkan cara menggunakan dalam perancang SerializeReadOnlyPropertyDescriptor kustom.

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

Keterangan

Deskripsi properti terdiri dari nama, atributnya, kelas komponen yang dikaitkan dengan properti, dan jenis properti .

PropertyDescriptor menyediakan properti dan metode berikut:

PropertyDescriptor juga menyediakan properti dan metode berikut abstract :

  • ComponentType berisi tipe komponen yang terikat dengan properti ini.

  • IsReadOnly menunjukkan apakah properti ini baca-saja.

  • PropertyType mendapatkan jenis properti .

  • CanResetValue menunjukkan apakah mengatur ulang komponen mengubah nilai komponen.

  • GetValue mengembalikan nilai properti saat ini pada komponen.

  • ResetValue mengatur ulang nilai untuk properti komponen ini.

  • SetValue mengatur nilai komponen ke nilai yang berbeda.

  • ShouldSerializeValue menunjukkan apakah nilai properti ini perlu dipertahankan.

Biasanya, anggota diimplementasikan abstract melalui refleksi. Untuk informasi selengkapnya tentang refleksi, lihat topik dalam Refleksi.

Konstruktor

PropertyDescriptor(MemberDescriptor)

Menginisialisasi instans PropertyDescriptor baru kelas dengan nama dan atribut dalam yang ditentukan MemberDescriptor.

PropertyDescriptor(MemberDescriptor, Attribute[])

Menginisialisasi instans PropertyDescriptor baru kelas dengan nama dalam atribut yang ditentukan MemberDescriptor dan di MemberDescriptor array dan Attribute .

PropertyDescriptor(String, Attribute[])

Menginisialisasi instans PropertyDescriptor baru kelas dengan nama dan atribut yang ditentukan.

Properti

AttributeArray

Mendapatkan atau mengatur array atribut.

(Diperoleh dari MemberDescriptor)
Attributes

Mendapatkan kumpulan atribut untuk anggota ini.

(Diperoleh dari MemberDescriptor)
Category

Mendapatkan nama kategori tempat anggota berada, seperti yang ditentukan dalam CategoryAttribute.

(Diperoleh dari MemberDescriptor)
ComponentType

Ketika ditimpa di kelas turunan, mendapatkan jenis komponen yang terikat dengan properti ini.

Converter

Mendapatkan pengonversi tipe untuk properti ini.

Description

Mendapatkan deskripsi anggota, seperti yang ditentukan dalam DescriptionAttribute.

(Diperoleh dari MemberDescriptor)
DesignTimeOnly

Mendapatkan apakah anggota ini harus diatur hanya pada waktu desain, seperti yang ditentukan dalam DesignOnlyAttribute.

(Diperoleh dari MemberDescriptor)
DisplayName

Mendapatkan nama yang bisa ditampilkan di jendela, seperti jendela Properti.

(Diperoleh dari MemberDescriptor)
IsBrowsable

Mendapatkan nilai yang menunjukkan apakah anggota dapat dijelajahi, seperti yang ditentukan dalam BrowsableAttribute.

(Diperoleh dari MemberDescriptor)
IsLocalizable

Mendapatkan nilai yang menunjukkan apakah properti ini harus dilokalkan, seperti yang ditentukan dalam LocalizableAttribute.

IsReadOnly

Ketika ditimpa di kelas turunan, mendapatkan nilai yang menunjukkan apakah properti ini baca-saja.

Name

Mendapatkan nama anggota.

(Diperoleh dari MemberDescriptor)
NameHashCode

Mendapatkan kode hash untuk nama anggota, seperti yang ditentukan dalam GetHashCode().

(Diperoleh dari MemberDescriptor)
PropertyType

Ketika ditimpa di kelas turunan, mendapatkan jenis properti .

SerializationVisibility

Mendapatkan nilai yang menunjukkan apakah properti ini harus diserialisasikan, seperti yang ditentukan dalam DesignerSerializationVisibilityAttribute.

SupportsChangeEvents

Mendapatkan nilai yang menunjukkan apakah pemberitahuan perubahan nilai untuk properti ini mungkin berasal dari luar pendeskripsi properti.

Metode

AddValueChanged(Object, EventHandler)

Mengaktifkan objek lain untuk diberi tahu ketika properti ini berubah.

CanResetValue(Object)

Saat ditimpa di kelas turunan, mengembalikan apakah mengatur ulang objek mengubah nilainya.

CreateAttributeCollection()

Membuat kumpulan atribut menggunakan array atribut yang diteruskan ke konstruktor.

(Diperoleh dari MemberDescriptor)
CreateInstance(Type)

Membuat instans dari jenis yang ditentukan.

Equals(Object)

Membandingkan ini dengan objek lain untuk melihat apakah objek tersebut setara.

FillAttributes(IList)

Menambahkan atribut PropertyDescriptor ke daftar atribut yang ditentukan di kelas induk.

FillAttributes(IList)

Saat ditimpa di kelas turunan, menambahkan atribut kelas pewarisan ke daftar atribut yang ditentukan di kelas induk.

(Diperoleh dari MemberDescriptor)
GetChildProperties()

Mengembalikan default PropertyDescriptorCollection.

GetChildProperties(Attribute[])

Mengembalikan PropertyDescriptorCollection menggunakan array atribut tertentu sebagai filter.

GetChildProperties(Object)

Mengembalikan PropertyDescriptorCollection untuk objek tertentu.

GetChildProperties(Object, Attribute[])

Mengembalikan PropertyDescriptorCollection untuk objek tertentu menggunakan array atribut tertentu sebagai filter.

GetEditor(Type)

Mendapatkan editor dari jenis yang ditentukan.

GetHashCode()

Mengembalikan kode hash untuk objek ini.

GetInvocationTarget(Type, Object)

Metode ini mengembalikan objek yang harus digunakan selama pemanggilan anggota.

GetInvocationTarget(Type, Object)

Mengambil objek yang harus digunakan selama pemanggilan anggota.

(Diperoleh dari MemberDescriptor)
GetType()

Mendapatkan dari instans Type saat ini.

(Diperoleh dari Object)
GetTypeFromName(String)

Mengembalikan jenis menggunakan namanya.

GetValue(Object)

Saat ditimpa di kelas turunan, mendapatkan nilai properti saat ini pada komponen.

GetValueChangedHandler(Object)

Mengambil set ValueChanged penanganan aktivitas saat ini untuk komponen tertentu.

MemberwiseClone()

Membuat salinan dangkal dari saat ini Object.

(Diperoleh dari Object)
OnValueChanged(Object, EventArgs)

ValueChanged Meningkatkan peristiwa yang Anda terapkan.

RemoveValueChanged(Object, EventHandler)

Mengaktifkan objek lain untuk diberi tahu ketika properti ini berubah.

ResetValue(Object)

Saat ditimpa di kelas turunan, mengatur ulang nilai untuk properti komponen ini ke nilai default.

SetValue(Object, Object)

Saat ditimpa di kelas turunan, mengatur nilai komponen ke nilai yang berbeda.

ShouldSerializeValue(Object)

Ketika ditimpa di kelas turunan, menentukan nilai yang menunjukkan apakah nilai properti ini perlu dipertahankan.

ToString()

Mengembalikan string yang mewakili objek saat ini.

(Diperoleh dari Object)

Berlaku untuk

Lihat juga