ReadOnlyAttributeとは? わかりやすく解説

ReadOnlyAttribute クラス

この属性が関連付けられているプロパティがデザイン時に読み取り専用か、または読み書き可能かを指定します。このクラスは継承できません。

名前空間: System.ComponentModel
アセンブリ: System (system.dll 内)
構文構文

<AttributeUsageAttribute(AttributeTargets.All)> _
Public NotInheritable Class
 ReadOnlyAttribute
    Inherits Attribute
Dim instance As ReadOnlyAttribute
[AttributeUsageAttribute(AttributeTargets.All)] 
public sealed class ReadOnlyAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::All)] 
public ref class ReadOnlyAttribute sealed :
 public Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.All) */ 
public final class ReadOnlyAttribute extends
 Attribute
AttributeUsageAttribute(AttributeTargets.All) 
public final class ReadOnlyAttribute extends
 Attribute
解説解説

ReadOnlyAttribute に true を設定してマークされているメンバや、Set メソッドを持たないメンバは変更できません。この属性を持たないメンバや、ReadOnlyAttribute に false を設定してマークされているメンバは、読み書き可能で、変更できます。既定値は No です。

メモ重要 :

PropertyDescriptor クラスは、デザイン環境で、および実行時に ReadOnlyAttribute を適用します。ReadOnlyAttribute に true を設定してプロパティをマークすると、この属性の値は定数メンバ Yes に設定されます。false に設定された ReadOnlyAttribute でマークしたプロパティの場合、値は No になります。したがって、コード内でこの属性の値を確認する場合は、属性を ReadOnlyAttribute.Yes または ReadOnlyAttribute.No として指定する必要があります。

詳細については、属性の概要、属性を使用したメタデータの拡張 の各トピックを参照してください。

使用例使用例

プロパティを読み取り専用としてマークするコード例を次に示します。

[ReadOnly(true)]
 public int MyProperty {
    get {
       // Insert code here.
       return 0;
    }
}
   [ReadOnly(true)]
   int get()
   {
      // Insert code here.
      return 0;
   }
}
/** @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.
}
AttributeCollection attributes = 
    TypeDescriptor.GetAttributes("MyProperty");

if (attributes.get_Item(ReadOnlyAttribute.class.ToType()).Equals(
    ReadOnlyAttribute.Yes)) {
    // Insert code here.
}
継承階層継承階層
System.Object
   System.Attribute
    System.ComponentModel.ReadOnlyAttribute
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

ReadOnlyAttribute コンストラクタ

ReadOnlyAttribute クラスの新しいインスタンスを初期化します。

名前空間: System.ComponentModel
アセンブリ: System (system.dll 内)
構文構文

Public Sub New ( _
    isReadOnly As Boolean _
)
Dim isReadOnly As Boolean

Dim instance As New ReadOnlyAttribute(isReadOnly)
public ReadOnlyAttribute (
    bool isReadOnly
)
public:
ReadOnlyAttribute (
    bool isReadOnly
)
public ReadOnlyAttribute (
    boolean isReadOnly
)
public function ReadOnlyAttribute (
    isReadOnly : boolean
)

パラメータ

isReadOnly

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

解説解説

ReadOnlyAttribute に true を設定してマークされているメンバや、Set メソッドを持たないメンバは変更できません。この属性を持たないメンバや、ReadOnlyAttribute に false を設定してマークされているメンバは、読み書き可能で、変更できます。既定値は No です。

メモメモ

ReadOnlyAttribute に true を設定してプロパティをマークすると、この属性の値は定数メンバ Yes に設定されます。false に設定された ReadOnlyAttribute でマークしたプロパティの場合、値は No になります。したがって、コード内でこの属性の値を確認する場合は、属性を ReadOnlyAttribute.Yes または ReadOnlyAttribute.No として指定する必要があります。

使用例使用例

プロパティを読み取り専用としてマークするコード例を次に示します。このコードは、新しい ReadOnlyAttribute を作成し、その値を ReadOnlyAttribute.Yes に設定してから、その属性をプロパティに関連付けます。

<ReadOnlyAttribute(True)> _
Public Property MyProperty() As
 Integer
    Get
        ' Insert code here.
        Return 0
    End Get
    Set
        ' Insert code here.
    End Set
End Property

[ReadOnly(true)]
public int MyProperty {
   get {
      // Insert code here.
      return 0;
   }
   set {
      // Insert code here.
   }
}
   
public:
   [ReadOnly(true)]
   property int MyProperty 
   {
      int get()
      {
         // Insert code here.
         return 0;
      }
      void set( int value
 )
      {
         // Insert code here.
      }
   }
/** @attribute ReadOnly(true)
 */
/** @property
 */
public int get_MyProperty()
{
    // Insert code here.
    return 0;
} //get_MyProperty

/** @property 
 */
public void set_MyProperty(int
 value)
{
    // Insert code here.
} //set_MyProperty
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ReadOnlyAttribute クラス
ReadOnlyAttribute メンバ
System.ComponentModel 名前空間
ReadOnlyAttribute クラス

ReadOnlyAttribute フィールド


ReadOnlyAttribute プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ IsReadOnly この属性が関連付けられているプロパティが読み取り専用かどうかを示す値を取得します。
パブリック プロパティ TypeId  派生クラスに実装されている場合は、この Attribute の一意の識別子を取得します。 ( Attribute から継承されます。)
参照参照

関連項目

ReadOnlyAttribute クラス
System.ComponentModel 名前空間
Attribute
PropertyDescriptor クラス
AttributeCollection クラス
PropertyDescriptorCollection クラス

ReadOnlyAttribute メソッド


パブリック メソッドパブリック メソッド

( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド Equals オーバーロードされます。 オーバーライドされます。 このインスタンスと指定したオブジェクトが等しいかどうかを示します。
パブリック メソッド GetCustomAttribute  オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用された指定した型のカスタム属性を取得します。 ( Attribute から継承されます。)
パブリック メソッド GetCustomAttributes  オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されたカスタム属性の配列を取得します。 ( Attribute から継承されます。)
パブリック メソッド GetHashCode オーバーライドされます。 対象のインスタンスのハッシュ コードを返します。
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 ( Object から継承されます。)
パブリック メソッド IsDefaultAttribute オーバーライドされます。 この属性が既定値かどうかを判断します。
パブリック メソッド IsDefined  オーバーロードされます。 指定した型のカスタム属性が、アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されているかどうかを判断します。 ( Attribute から継承されます。)
パブリック メソッド Match  派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 ( Attribute から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。)
パブリック メソッド ToString  現在の Object を表す String を返します。 ( Object から継承されます。)
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

ReadOnlyAttribute クラス
System.ComponentModel 名前空間
Attribute
PropertyDescriptor クラス
AttributeCollection クラス
PropertyDescriptorCollection クラス

ReadOnlyAttribute メンバ

この属性が関連付けられているプロパティがデザイン時に読み取り専用か、または読み書き可能かを指定します。このクラスは継承できません。

ReadOnlyAttribute データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド ReadOnlyAttribute ReadOnlyAttribute クラスの新しいインスタンスを初期化します。
パブリック フィールドパブリック フィールド
パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ IsReadOnly この属性が関連付けられているプロパティが読み取り専用かどうかを示す値を取得します。
パブリック プロパティ TypeId  派生クラスに実装されている場合は、この Attribute の一意の識別子を取得します。(Attribute から継承されます。)
パブリック メソッドパブリック メソッド
( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド Equals オーバーロードされます。 オーバーライドされます。 このインスタンスと指定したオブジェクトが等しいかどうかを示します。
パブリック メソッド GetCustomAttribute  オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用された指定した型のカスタム属性を取得します。 (Attribute から継承されます。)
パブリック メソッド GetCustomAttributes  オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されたカスタム属性の配列を取得します。 (Attribute から継承されます。)
パブリック メソッド GetHashCode オーバーライドされます。 対象のインスタンスのハッシュ コードを返します。
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 (Object から継承されます。)
パブリック メソッド IsDefaultAttribute オーバーライドされます。 この属性が既定値かどうかを判断します。
パブリック メソッド IsDefined  オーバーロードされます。 指定した型のカスタム属性が、アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されているかどうかを判断します。 (Attribute から継承されます。)
パブリック メソッド Match  派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 (Attribute から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。)
パブリック メソッド ToString  現在の Object を表す String を返します。 (Object から継承されます。)
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

ReadOnlyAttribute クラス
System.ComponentModel 名前空間
Attribute
PropertyDescriptor クラス
AttributeCollection クラス
PropertyDescriptorCollection クラス



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「ReadOnlyAttribute」の関連用語

ReadOnlyAttributeのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ReadOnlyAttributeのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS