PropertyDescriptor Osztály
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Egy osztály tulajdonságának absztrakcióját biztosítja.
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
- Öröklődés
- Származtatott
- Attribútumok
Példák
Az alábbi példakód az osztályban lévő példára PropertyDescriptorCollection épül. Kinyomtatja a szövegmezőben lévő gomb szövegének adatait (kategória, leírás, megjelenítendő név). Ezt feltételezi, button1 és textbox1 egy űrlapon példányosították.
// 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.
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 & ControlChars.Cr
TextBox1.Text += myProperty.Description & ControlChars.Cr
TextBox1.Text += myProperty.Category & ControlChars.Cr
Az alábbi példakód bemutatja, hogyan implementálhat egy egyéni tulajdonságleírót, amely írásvédett burkolót biztosít egy tulajdonság köré. Az SerializeReadOnlyPropertyDescriptor egyéni tervezőben a vezérlő tulajdonságának írásvédett tulajdonságleírója Size használható.
using System;
using System.Collections;
using System.ComponentModel;
namespace ReadOnlyPropertyDescriptorTest;
// The SerializeReadOnlyPropertyDescriptor shows how to implement a
// custom property descriptor. It provides a read-only wrapper
// around the specified PropertyDescriptor.
sealed class SerializeReadOnlyPropertyDescriptor : PropertyDescriptor
{
readonly PropertyDescriptor _pd;
public SerializeReadOnlyPropertyDescriptor(PropertyDescriptor pd)
: base(pd) => _pd = pd;
public override AttributeCollection Attributes => AppendAttributeCollection(
_pd.Attributes,
ReadOnlyAttribute.Yes);
protected override void FillAttributes(IList attributeList) => attributeList.Add(ReadOnlyAttribute.Yes);
public override Type ComponentType => _pd.ComponentType;
// The type converter for this property.
// A translator can overwrite with its own converter.
public override TypeConverter Converter => _pd.Converter;
// Returns the property editor
// A translator can overwrite with its own editor.
public override object GetEditor(Type editorBaseType) => _pd.GetEditor(editorBaseType);
// Specifies the property is read only.
public override bool IsReadOnly => true;
public override Type PropertyType => _pd.PropertyType;
public override bool CanResetValue(object component) => _pd.CanResetValue(component);
public override object GetValue(object component) => _pd.GetValue(component);
public override void ResetValue(object component) => _pd.ResetValue(component);
public override void SetValue(object component, object val) => _pd.SetValue(component, val);
// Determines whether a value should be serialized.
public override bool ShouldSerializeValue(object component)
{
bool result = _pd.ShouldSerializeValue(component);
if (!result)
{
DefaultValueAttribute dva = (DefaultValueAttribute)_pd.Attributes[typeof(DefaultValueAttribute)];
result = dva == null || !Equals(_pd.GetValue(component), dva.Value);
}
return result;
}
// The following Utility methods create a new AttributeCollection
// by appending the specified attributes to an existing collection.
public static AttributeCollection AppendAttributeCollection(
AttributeCollection existing,
params Attribute[] newAttrs) => new(AppendAttributes(existing, newAttrs));
public static Attribute[] AppendAttributes(
AttributeCollection existing,
params Attribute[] newAttrs)
{
if (existing == null)
{
throw new ArgumentNullException(nameof(existing));
}
newAttrs ??= [];
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(nameof(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
Az alábbi példakódok bemutatják, hogyan használható az SerializeReadOnlyPropertyDescriptor egyéni tervezőben.
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(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.Windows.Forms
<Designer(GetType(DemoControlDesigner))>
Public Class DemoControl
Inherits Control
Public Sub New()
End Sub
End Class
Megjegyzések
A tulajdonság leírása egy névből, annak attribútumaiból, a tulajdonsághoz társított összetevőosztályból és a tulajdonság típusából áll.
PropertyDescriptor a következő tulajdonságokat és metódusokat biztosítja:
Converter TypeConverter a tulajdonság értékét tartalmazza.
IsLocalizable azt jelzi, hogy ezt a tulajdonságot honosítandó-e.
GetEditor A megadott típusú szerkesztőt adja vissza.
PropertyDescriptor a következő abstract tulajdonságokat és metódusokat is biztosítja:
ComponentType A tulajdonsághoz kötött összetevő típusát tartalmazza.
IsReadOnly azt jelzi, hogy ez a tulajdonság írásvédett-e.
PropertyType lekéri a tulajdonság típusát.
CanResetValue azt jelzi, hogy az összetevő alaphelyzetbe állítása módosítja-e az összetevő értékét.
GetValue Egy összetevő tulajdonságának aktuális értékét adja vissza.
ResetValue alaphelyzetbe állítja az összetevő tulajdonságának értékét.
SetValue az összetevő értékét egy másik értékre állítja.
ShouldSerializeValue azt jelzi, hogy a tulajdonság értékét meg kell-e őrizni.
A tagok általában tükröződésen abstract keresztül valósulnak meg. A tükröződéssel kapcsolatos további információkért tekintse meg a Tükröződés témakörét.
Konstruktorok
| Name | Description |
|---|---|
| PropertyDescriptor(MemberDescriptor, Attribute[]) |
Inicializálja az PropertyDescriptor osztály új példányát a megadott MemberDescriptor névvel és a tömb attribútumaival MemberDescriptorAttribute . |
| PropertyDescriptor(MemberDescriptor) |
Inicializálja az PropertyDescriptor osztály új példányát a megadott MemberDescriptornévvel és attribútumokkal. |
| PropertyDescriptor(String, Attribute[]) |
Inicializálja az PropertyDescriptor osztály új példányát a megadott névvel és attribútumokkal. |
Tulajdonságok
| Name | Description |
|---|---|
| AttributeArray |
Attribútumok tömbjének lekérdezése vagy beállítása. (Öröklődés forrása MemberDescriptor) |
| Attributes |
Lekéri a tag attribútumgyűjteményét. (Öröklődés forrása MemberDescriptor) |
| Category |
Lekéri annak a kategóriának a nevét, amelyhez a tag tartozik, a CategoryAttributemegadott módon. (Öröklődés forrása MemberDescriptor) |
| ComponentType |
Ha egy származtatott osztályban felül van bírálva, lekéri annak az összetevőnek a típusát, amelyhez a tulajdonság kötődik. |
| Converter |
Lekéri a tulajdonság típuskonverterét. |
| ConverterFromRegisteredType |
Lekéri a tulajdonság típuskonverterét. |
| Description |
Lekéri a tag leírását a DescriptionAttribute. (Öröklődés forrása MemberDescriptor) |
| DesignTimeOnly |
Lekérdezi, hogy ezt a tagot csak a tervezéskor kell-e beállítani, a megadott módon.DesignOnlyAttribute (Öröklődés forrása MemberDescriptor) |
| DisplayName |
Lekéri az ablakban megjeleníthető nevet, például egy Tulajdonságok ablak. (Öröklődés forrása MemberDescriptor) |
| IsBrowsable |
Lekéri az értéket, amely jelzi, hogy a tag böngészhető-e, a BrowsableAttributemegadott módon. (Öröklődés forrása MemberDescriptor) |
| IsLocalizable |
Beolvas egy értéket, amely jelzi, hogy a tulajdonság honosított legyen-e a LocalizableAttribute. |
| IsReadOnly |
Ha egy származtatott osztályban felül van bírálva, egy értéket kap, amely jelzi, hogy ez a tulajdonság írásvédett-e. |
| Name |
Megkapja a tag nevét. (Öröklődés forrása MemberDescriptor) |
| NameHashCode |
Lekéri a tag nevének kivonatkódját a megadott módon GetHashCode(). (Öröklődés forrása MemberDescriptor) |
| PropertyType |
Ha felül van bírálva egy származtatott osztályban, lekéri a tulajdonság típusát. |
| SerializationVisibility |
Beolvas egy értéket, amely jelzi, hogy ezt a tulajdonságot szerializálni kell-e a DesignerSerializationVisibilityAttribute. |
| SupportsChangeEvents |
Lekérdez egy értéket, amely jelzi, hogy a tulajdonság értékváltozási értesítései a tulajdonságleírón kívülről származhatnak-e. |
Metódusok
| Name | Description |
|---|---|
| AddValueChanged(Object, EventHandler) |
Lehetővé teszi más objektumok értesítését a tulajdonság megváltozásakor. |
| CanResetValue(Object) |
Származtatott osztály felülírásakor azt adja vissza, hogy egy objektum alaphelyzetbe állítása módosítja-e az értékét. |
| CreateAttributeCollection() |
Attribútumgyűjteményt hoz létre a konstruktornak átadott attribútumok tömbjének használatával. (Öröklődés forrása MemberDescriptor) |
| CreateInstance(Type) |
Létrehozza a megadott típusú példányt. |
| Equals(Object) |
Összehasonlítja ezt egy másik objektummal annak megtekintéséhez, hogy azok egyenértékűek-e. |
| FillAttributes(IList) |
Hozzáadja az attribútumokat PropertyDescriptor a szülőosztály megadott attribútumlistájához. |
| FillAttributes(IList) |
Ha egy származtatott osztályban felül van bírálva, hozzáadja az öröklő osztály attribútumait a szülőosztály megadott attribútumlistájához. (Öröklődés forrása MemberDescriptor) |
| GetChildProperties() |
Az alapértelmezett PropertyDescriptorCollectionértéket adja vissza. |
| GetChildProperties(Attribute[]) |
PropertyDescriptorCollection Egy megadott attribútumtömb szűrőként való használatát adja vissza. |
| GetChildProperties(Object, Attribute[]) |
PropertyDescriptorCollection Egy adott objektum egy megadott attribútumtömbjének szűrőként való visszaadása. |
| GetChildProperties(Object) |
Egy adott objektumhoz ad vissza egy PropertyDescriptorCollection értéket. |
| GetEditor(Type) |
Lekéri a megadott típusú szerkesztőt. |
| GetHashCode() |
Az objektum kivonatkódját adja vissza. |
| GetInvocationTarget(Type, Object) |
Ez a metódus a tagok meghívása során használandó objektumot adja vissza. |
| GetType() |
Lekéri az Type aktuális példányt. (Öröklődés forrása Object) |
| GetTypeFromName(String) |
Egy típust ad vissza a nevével. |
| GetValue(Object) |
Ha felül van bírálva egy származtatott osztályban, lekéri a tulajdonság aktuális értékét egy összetevőn. |
| GetValueChangedHandler(Object) |
Lekéri egy adott összetevő eseménykezelőinek |
| MemberwiseClone() |
Az aktuális Objectpéldány sekély másolatát hozza létre. (Öröklődés forrása Object) |
| OnValueChanged(Object, EventArgs) |
|
| RemoveValueChanged(Object, EventHandler) |
Lehetővé teszi más objektumok értesítését a tulajdonság megváltozásakor. |
| ResetValue(Object) |
Ha egy származtatott osztályban felül van bírálva, az összetevő ezen tulajdonságának értékét visszaállítja az alapértelmezett értékre. |
| SetValue(Object, Object) |
Ha egy származtatott osztályban felül van bírálva, az összetevő értékét egy másik értékre állítja. |
| ShouldSerializeValue(Object) |
Ha egy származtatott osztályban felül van bírálva, meghatároz egy értéket, amely jelzi, hogy a tulajdonság értékét meg kell-e őrizni. |
| ToString() |
Az aktuális objektumot jelképező sztringet ad vissza. (Öröklődés forrása Object) |