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
解説解説

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

メモ重要 :

PropertyDescriptor クラスは、デザイン環境で、および実行時ReadOnlyAttribute適用します。ReadOnlyAttributetrue設定してプロパティマークすると、この属性の値は定数メンバ 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

MyPropertyReadOnlyAttribute の値を確認する方法次のコード例示します最初にオブジェクトすべてのプロパティ保持する 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) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「ReadOnlyAttribute クラス」の関連用語

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

   

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



ReadOnlyAttribute クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS