PropertyDescriptor 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
클래스에서 속성의 추상화 기능을 제공합니다.
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
- 상속
- 파생
- 특성
예제
다음 코드 예제는 클래스의 예제를 기반으로 합니다 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.
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
다음 코드 예제에서는 속성 주위에 읽기 전용 래퍼를 제공하는 사용자 지정 속성 설명자를 구현하는 방법을 보여줍니다. 사용자 SerializeReadOnlyPropertyDescriptor 지정 디자이너에서 컨트롤 Size 의 속성에 대한 읽기 전용 속성 설명자를 제공하는 데 사용됩니다.
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
다음 코드 예제에서는 사용자 지정 디자이너에서 사용 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(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
설명
속성에 대한 설명은 이름, 해당 특성, 속성과 연결된 구성 요소 클래스 및 속성의 형식으로 구성됩니다.
PropertyDescriptor 에서는 다음과 같은 속성과 메서드를 제공합니다.
Converter 에는 이 속성이 TypeConverter 포함됩니다.
IsLocalizable 는 이 속성을 지역화해야 하는지 여부를 나타냅니다.
GetEditor 는 지정된 형식의 편집기를 반환합니다.
PropertyDescriptor 에서는 다음과 같은 abstract 속성 및 메서드도 제공합니다.
ComponentType 에는 이 속성이 바인딩된 구성 요소의 형식이 포함됩니다.
IsReadOnly 는 이 속성이 읽기 전용인지 여부를 나타냅니다.
PropertyType 속성의 형식을 가져옵니다.
CanResetValue 는 구성 요소를 다시 설정하면 구성 요소의 값이 변경되는지 여부를 나타냅니다.
GetValue 는 구성 요소에 있는 속성의 현재 값을 반환합니다.
ResetValue 구성 요소의 이 속성 값을 다시 설정합니다.
SetValue 는 구성 요소의 값을 다른 값으로 설정합니다.
ShouldSerializeValue 는 이 속성의 값을 유지해야 하는지 여부를 나타냅니다.
일반적으로 멤버는 abstract 리플렉션을 통해 구현됩니다. 리플렉션에 대한 자세한 내용은 리플렉션의 항목을 참조하세요.
생성자
| Name | Description |
|---|---|
| PropertyDescriptor(MemberDescriptor, Attribute[]) |
지정된 MemberDescriptor 이름과 배열의 특성을 사용하여 클래스의 PropertyDescriptor 새 인스턴스를 MemberDescriptorAttribute 초기화합니다. |
| PropertyDescriptor(MemberDescriptor) |
지정된 이름 및 특성을 사용하여 클래스의 PropertyDescriptor 새 인스턴스를 초기화합니다 MemberDescriptor. |
| PropertyDescriptor(String, Attribute[]) |
지정된 이름과 특성을 사용하여 클래스의 PropertyDescriptor 새 인스턴스를 초기화합니다. |
속성
| Name | Description |
|---|---|
| AttributeArray |
특성 배열을 가져오거나 설정합니다. (다음에서 상속됨 MemberDescriptor) |
| Attributes |
이 멤버에 대한 특성 컬렉션을 가져옵니다. (다음에서 상속됨 MemberDescriptor) |
| Category |
에 지정된 대로 멤버가 속한 범주의 CategoryAttribute이름을 가져옵니다. (다음에서 상속됨 MemberDescriptor) |
| ComponentType |
파생 클래스에서 재정의되는 경우 이 속성이 바인딩된 구성 요소의 형식을 가져옵니다. |
| Converter |
이 속성의 형식 변환기를 가져옵니다. |
| ConverterFromRegisteredType |
이 속성의 형식 변환기를 가져옵니다. |
| Description |
에 지정된 DescriptionAttribute대로 멤버에 대한 설명을 가져옵니다. (다음에서 상속됨 MemberDescriptor) |
| DesignTimeOnly |
에 지정된 DesignOnlyAttribute대로 디자인 타임에만 이 멤버를 설정할지 여부를 가져옵니다. (다음에서 상속됨 MemberDescriptor) |
| DisplayName |
속성 창과 같이 창에 표시할 수 있는 이름을 가져옵니다. (다음에서 상속됨 MemberDescriptor) |
| IsBrowsable |
에 지정된 BrowsableAttribute대로 멤버를 검색할 수 있는지 여부를 나타내는 값을 가져옵니다. (다음에서 상속됨 MemberDescriptor) |
| IsLocalizable |
에 지정된 LocalizableAttribute대로 이 속성을 지역화해야 하는지 여부를 나타내는 값을 가져옵니다. |
| IsReadOnly |
파생 클래스에서 재정의되는 경우 이 속성이 읽기 전용인지 여부를 나타내는 값을 가져옵니다. |
| Name |
멤버의 이름을 가져옵니다. (다음에서 상속됨 MemberDescriptor) |
| NameHashCode |
에 지정된 GetHashCode()멤버 이름에 대한 해시 코드를 가져옵니다. (다음에서 상속됨 MemberDescriptor) |
| PropertyType |
파생 클래스에서 재정의되는 경우 속성의 형식을 가져옵니다. |
| SerializationVisibility |
에 지정된 DesignerSerializationVisibilityAttribute대로 이 속성을 serialize해야 하는지 여부를 나타내는 값을 가져옵니다. |
| SupportsChangeEvents |
이 속성에 대한 값 변경 알림이 속성 설명자 외부에서 발생할 수 있는지 여부를 나타내는 값을 가져옵니다. |
메서드
| Name | Description |
|---|---|
| AddValueChanged(Object, EventHandler) |
이 속성이 변경될 때 다른 개체에 알림을 받을 수 있도록 합니다. |
| CanResetValue(Object) |
파생 클래스에서 재정의되는 경우 개체를 다시 설정하면 해당 값이 변경되는지 여부를 반환합니다. |
| CreateAttributeCollection() |
생성자에 전달된 특성 배열을 사용하여 특성 컬렉션을 만듭니다. (다음에서 상속됨 MemberDescriptor) |
| CreateInstance(Type) |
지정된 형식의 인스턴스를 만듭니다. |
| Equals(Object) |
이 개체를 다른 개체와 비교하여 동일한지 확인합니다. |
| FillAttributes(IList) |
부모 클래스의 PropertyDescriptor 지정된 특성 목록에 특성을 추가합니다. |
| FillAttributes(IList) |
파생 클래스에서 재정의되는 경우 상속 클래스의 특성을 부모 클래스의 지정된 특성 목록에 추가합니다. (다음에서 상속됨 MemberDescriptor) |
| GetChildProperties() |
기본값 PropertyDescriptorCollection을 반환합니다. |
| GetChildProperties(Attribute[]) |
PropertyDescriptorCollection 지정된 특성 배열을 필터로 사용하여 반환합니다. |
| GetChildProperties(Object, Attribute[]) |
PropertyDescriptorCollection 지정된 특성 배열을 필터로 사용하여 지정된 개체에 대한 값을 반환합니다. |
| GetChildProperties(Object) |
PropertyDescriptorCollection 지정된 개체에 대한 값을 반환합니다. |
| GetEditor(Type) |
지정된 형식의 편집기를 가져옵니다. |
| GetHashCode() |
이 개체의 해시 코드를 반환합니다. |
| GetInvocationTarget(Type, Object) |
이 메서드는 멤버를 호출하는 동안 사용해야 하는 개체를 반환합니다. |
| GetType() |
현재 인스턴스의 Type 가져옵니다. (다음에서 상속됨 Object) |
| GetTypeFromName(String) |
해당 이름을 사용하여 형식을 반환합니다. |
| GetValue(Object) |
파생 클래스에서 재정의되는 경우 구성 요소에 있는 속성의 현재 값을 가져옵니다. |
| GetValueChangedHandler(Object) |
특정 구성 요소에 대한 현재 이벤트 처리기 집합 |
| MemberwiseClone() |
현재 Object단순 복사본을 만듭니다. (다음에서 상속됨 Object) |
| OnValueChanged(Object, EventArgs) |
구현한 |
| RemoveValueChanged(Object, EventHandler) |
이 속성이 변경될 때 다른 개체에 알림을 받을 수 있도록 합니다. |
| ResetValue(Object) |
파생 클래스에서 재정의되는 경우 구성 요소의 이 속성 값을 기본값으로 다시 설정합니다. |
| SetValue(Object, Object) |
파생 클래스에서 재정의되는 경우 구성 요소의 값을 다른 값으로 설정합니다. |
| ShouldSerializeValue(Object) |
파생 클래스에서 재정의되는 경우 이 속성의 값을 유지해야 하는지 여부를 나타내는 값을 결정합니다. |
| ToString() |
현재 개체를 나타내는 문자열을 반환합니다. (다음에서 상속됨 Object) |