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 기반으로 합니다. 텍스트 상자에 단추 텍스트의 정보(범주, 설명, 표시 이름)를 인쇄합니다. 폼에서 button1
textbox1
인스턴스화되었다고 가정합니다.
// 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 에서는 다음과 같은 속성과 메서드를 제공합니다.
Converter 에는 이 속성이 TypeConverter 포함됩니다.
IsLocalizable 는 이 속성을 지역화해야 하는지 여부를 나타냅니다.
GetEditor 는 지정된 형식의 편집기를 반환합니다.
PropertyDescriptor 또한 다음과 같은 abstract
속성과 메서드를 제공합니다.
ComponentType 에는 이 속성이 바인딩된 구성 요소의 형식이 포함됩니다.
IsReadOnly 는 이 속성이 읽기 전용인지 여부를 나타냅니다.
PropertyType 는 속성의 형식을 가져옵니다.
CanResetValue 는 구성 요소를 다시 설정하면 구성 요소의 값이 변경되는지 여부를 나타냅니다.
GetValue 는 구성 요소에서 속성의 현재 값을 반환합니다.
ResetValue 는 구성 요소의 이 속성 값을 다시 설정합니다.
SetValue 는 구성 요소의 값을 다른 값으로 설정합니다.
ShouldSerializeValue 는 이 속성의 값을 유지해야 하는지 여부를 나타냅니다.
일반적으로 멤버는 abstract
리플렉션을 통해 구현됩니다. 리플렉션에 대한 자세한 내용은 리플렉션의 항목을 참조하세요.
생성자
PropertyDescriptor(MemberDescriptor) |
지정된 PropertyDescriptor의 이름과 특성을 사용하여 MemberDescriptor 클래스의 새 인스턴스를 초기화합니다. |
PropertyDescriptor(MemberDescriptor, Attribute[]) |
지정된 PropertyDescriptor의 이름과 MemberDescriptor 및 MemberDescriptor 배열 모두의 특성을 사용하여 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에 지정된, 이 속성이 serialize되어야 하는지 여부를 나타내는 값을 가져옵니다. |
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) |
특정 구성 요소에 대한 |
MemberwiseClone() |
현재 Object의 단순 복사본을 만듭니다. (다음에서 상속됨 Object) |
OnValueChanged(Object, EventArgs) |
구현된 |
RemoveValueChanged(Object, EventHandler) |
이 속성이 변경되면 다른 개체에서 알림을 받을 수 있도록 합니다. |
ResetValue(Object) |
파생 클래스에서 재정의된 경우 구성 요소의 이 속성 값을 기본값으로 다시 설정합니다. |
SetValue(Object, Object) |
파생 클래스에서 재정의된 경우 구성 요소의 값을 다른 값으로 설정합니다. |
ShouldSerializeValue(Object) |
파생 클래스에서 재정의된 경우 이 속성 값이 지속되어야 하는지 여부를 나타내는 값을 확인합니다. |
ToString() |
현재 개체를 나타내는 문자열을 반환합니다. (다음에서 상속됨 Object) |