ReadOnlyAttribute クラス
アセンブリ: System (system.dll 内)

<AttributeUsageAttribute(AttributeTargets.All)> _ Public NotInheritable Class ReadOnlyAttribute Inherits Attribute
[AttributeUsageAttribute(AttributeTargets::All)] public ref class ReadOnlyAttribute sealed : public Attribute

ReadOnlyAttribute に true を設定してマークされているメンバや、Set メソッドを持たないメンバは変更できません。この属性を持たないメンバや、ReadOnlyAttribute に false を設定してマークされているメンバは、読み書き可能で、変更できます。既定値は No です。
![]() |
---|
PropertyDescriptor クラスは、デザイン環境で、および実行時に ReadOnlyAttribute を適用します。ReadOnlyAttribute に true を設定してプロパティをマークすると、この属性の値は定数メンバ Yes に設定されます。false に設定された ReadOnlyAttribute でマークしたプロパティの場合、値は No になります。したがって、コード内でこの属性の値を確認する場合は、属性を ReadOnlyAttribute.Yes または ReadOnlyAttribute.No として指定する必要があります。 |

プロパティを読み取り専用としてマークするコード例を次に示します。
Public ReadOnly Property MyProperty() As Integer Get ' Insert code here. Return 0 End Get End Property
/** @attribute ReadOnly(true) */ /** @property */ public int get_MyProperty() { // Insert code here. return 0; } //get_MyProperty
MyProperty の ReadOnlyAttribute の値を確認する方法を次のコード例に示します。最初に、オブジェクトのすべてのプロパティを保持する PropertyDescriptorCollection を取得します。次に、インデックスを付けて PropertyDescriptorCollection から MyProperty を取得します。そして、このプロパティの属性を返し、その属性を属性変数に保存します。
この例では、ReadOnlyAttribute の値を確認する 2 種類の方法を示します。2 番目のコード片では、Equals メソッドを呼び出します。最後のコード片では、IsReadOnly プロパティを使用して値を確認します。
' Gets the attributes for the property. Dim attributes As AttributeCollection = _ TypeDescriptor.GetProperties(Me)("MyProperty").Attributes ' Checks to see whether the value of the ReadOnlyAttribute is Yes. If attributes(GetType(ReadOnlyAttribute)).Equals(ReadOnlyAttribute.Yes) Then ' Insert code here. End If ' This is another way 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
// Gets the attributes for the property. AttributeCollection attributes = TypeDescriptor.GetProperties(this)["MyProperty"].Attributes; // Checks to see whether the value of the ReadOnlyAttribute is Yes. if(attributes[typeof(ReadOnlyAttribute)].Equals(ReadOnlyAttribute.Yes)) { // Insert code here. } // This is another way 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. AttributeCollection^ attributes = TypeDescriptor::GetProperties( this )[ "MyProperty" ]->Attributes; // Checks to see whether the value of the ReadOnlyAttribute is Yes. if ( attributes[ ReadOnlyAttribute::typeid ]->Equals( ReadOnlyAttribute::Yes ) ) { // Insert code here. } // This is another way 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). get_Item("MyProperty").get_Attributes(); // Checks to see whether the value of the ReadOnlyAttribute is Yes. if (attributes.get_Item(ReadOnlyAttribute.class.ToType()).Equals (ReadOnlyAttribute.Yes)) { // Insert code here. } // This is another way to see whether the property is read-only. ReadOnlyAttribute myAttribute = ((ReadOnlyAttribute) (attributes.get_Item(ReadOnlyAttribute.class.ToType()))); if (myAttribute.get_IsReadOnly()) { // Insert code here. }
ReadOnlyAttribute を使用してクラスをマークした場合は、次のコード例を使用して値を確認します。
Dim attributes As AttributeCollection = TypeDescriptor.GetAttributes(MyProperty) If attributes(GetType(ReadOnlyAttribute)).Equals(ReadOnlyAttribute.Yes) Then ' Insert code here. End If
AttributeCollection attributes = TypeDescriptor.GetAttributes(MyProperty); if(attributes[typeof(ReadOnlyAttribute)].Equals(ReadOnlyAttribute.Yes)) { // Insert code here. }
AttributeCollection^ attributes = TypeDescriptor::GetAttributes( MyProperty ); if ( attributes[ ReadOnlyAttribute::typeid ]->Equals( ReadOnlyAttribute::Yes ) ) { // Insert code here. }

System.Attribute
System.ComponentModel.ReadOnlyAttribute


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ReadOnlyAttribute メンバ
System.ComponentModel 名前空間
Attribute
PropertyDescriptor クラス
AttributeCollection クラス
PropertyDescriptorCollection クラス
- ReadOnlyAttribute クラスのページへのリンク