PropertyDescriptor 类

定义

提供类上属性的抽象。

C#
public abstract class PropertyDescriptor : System.ComponentModel.MemberDescriptor
C#
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class PropertyDescriptor : System.ComponentModel.MemberDescriptor
继承
PropertyDescriptor
派生
属性

示例

以下代码示例基于 PropertyDescriptorCollection 类中的示例生成。 它打印文本框中按钮文本的信息(类别、说明、显示名称)。 它假定在窗体上实例化了 button1textbox1

C#
// 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';

下面的代码示例演示如何实现一个自定义属性描述符,该描述符围绕属性提供只读包装器。 自定义设计器中使用 SerializeReadOnlyPropertyDescriptor 为控件的 Size 属性提供只读属性描述符。

C#
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;
        }
    }
}

以下代码示例演示如何在自定义设计器中使用 SerializeReadOnlyPropertyDescriptor

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

namespace ReadOnlyPropertyDescriptorTest
{
    [Designer(typeof(DemoControlDesigner))]
    public class DemoControl : Control
    {
        public DemoControl()
        {
        }
    }
}

注解

属性的说明包括名称、其属性、属性关联的组件类以及属性的类型。

PropertyDescriptor 提供以下属性和方法:

PropertyDescriptor 还提供以下 abstract 属性和方法:

通常,abstract 成员是通过反射实现的。 有关反射的详细信息,请参阅 反射中的主题。

构造函数

PropertyDescriptor(MemberDescriptor)

使用指定 MemberDescriptor中的名称和属性初始化 PropertyDescriptor 类的新实例。

PropertyDescriptor(MemberDescriptor, Attribute[])

使用指定 MemberDescriptor 中的名称以及 MemberDescriptorAttribute 数组中的属性初始化 PropertyDescriptor 类的新实例。

PropertyDescriptor(String, Attribute[])

使用指定的名称和属性初始化 PropertyDescriptor 类的新实例。

属性

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中指定的方式序列化此属性。

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)

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另请参阅