英語で読む

次の方法で共有


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 には、次のプロパティとメソッドが用意されています。

  • Converter には、このプロパティの TypeConverter が含まれています。

  • IsLocalizable は、このプロパティをローカライズする必要があるかどうかを示します。

  • GetEditor は、指定した型のエディターを返します。

PropertyDescriptor には、次の abstract のプロパティとメソッドも用意されています。

  • ComponentType には、このプロパティがバインドされているコンポーネントの型が含まれています。

  • IsReadOnly は、このプロパティが読み取り専用かどうかを示します。

  • PropertyType プロパティの型を取得します。

  • CanResetValue は、コンポーネントをリセットするとコンポーネントの値が変更されるかどうかを示します。

  • GetValue は、コンポーネントのプロパティの現在の値を返します。

  • ResetValue コンポーネントのこのプロパティの値をリセットします。

  • SetValue コンポーネントの値を別の値に設定します。

  • ShouldSerializeValue は、このプロパティの値を永続化する必要があるかどうかを示します。

通常、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

こちらもご覧ください