ReadOnlyAttribute.IsReadOnly プロパティ

定義

この属性が関連付けられているプロパティが読み取り専用かどうかを示す値を取得します。

public:
 property bool IsReadOnly { bool get(); };
public bool IsReadOnly { get; }
member this.IsReadOnly : bool
Public ReadOnly Property IsReadOnly As Boolean

プロパティ値

この属性が関連付けられているプロパティが読み取り専用の場合は true。プロパティが読み書き可能の場合は false

次のコード例では、 が読み取り専用かどうかを MyProperty 確認します。 まず、次の操作を行って、 の MyProperty 属性を取得します。

  • PropertyDescriptorCollectionオブジェクトのすべてのプロパティを含む を取得しています。

  • を取得MyPropertyするための へのPropertyDescriptorCollectionインデックス作成。

  • このプロパティの属性を attributes 変数に保存します。

次に、コードは 内AttributeCollectionの のReadOnlyAttribute値に設定myAttributeし、プロパティが読み取り専用かどうかを確認します。

// Gets the attributes for the property.
AttributeCollection^ attributes = TypeDescriptor::GetProperties( this )[ "MyProperty" ]->Attributes;

// Checks to see whether the property is read-only.
ReadOnlyAttribute^ myAttribute = dynamic_cast<ReadOnlyAttribute^>(attributes[ ReadOnlyAttribute::typeid ]);
if ( myAttribute->IsReadOnly )
{
   // Insert code here.
}
// Gets the attributes for the property.
AttributeCollection attributes = 
   TypeDescriptor.GetProperties(this)["MyProperty"].Attributes;

// Checks to see whether the property is read-only.
ReadOnlyAttribute myAttribute = 
   (ReadOnlyAttribute)attributes[typeof(ReadOnlyAttribute)];

if(myAttribute.IsReadOnly) {
   // Insert code here.
}
' Gets the attributes for the property.
Dim attributes As AttributeCollection = _
   TypeDescriptor.GetProperties(Me)("MyProperty").Attributes
       
' Checks to see whether the property is read-only.
Dim myAttribute As ReadOnlyAttribute = _
   CType(attributes(GetType(ReadOnlyAttribute)), ReadOnlyAttribute)
   
If myAttribute.IsReadOnly Then
    ' Insert code here.
End If

適用対象

こちらもご覧ください