ReadOnlyAttribute クラス

定義

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

public ref class ReadOnlyAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.All)]
public sealed class ReadOnlyAttribute : Attribute
public sealed class ReadOnlyAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.All)>]
type ReadOnlyAttribute = class
    inherit Attribute
type ReadOnlyAttribute = class
    inherit Attribute
Public NotInheritable Class ReadOnlyAttribute
Inherits Attribute
継承
ReadOnlyAttribute
属性

次のコード例では、プロパティを読み取り専用としてマークします。

   [ReadOnly(true)]
   int get()
   {
      // Insert code here.
      return 0;
   }
}
[ReadOnly(true)]
 public int MyProperty {
    get {
       // Insert code here.
       return 0;
    }
}
Public ReadOnly Property MyProperty() As Integer
    Get
        ' Insert code here.
        Return 0
    End Get
End Property

次のコード例では、 の値をチェックする方法をReadOnlyAttributeMyProperty示します。 最初に、コードは オブジェクトのすべてのプロパティを持つ を取得 PropertyDescriptorCollection します。 次に、 に PropertyDescriptorCollection インデックスを作成して を取得 MyPropertyします。 次に、このプロパティの属性を返し、属性変数に保存します。

この例では、 の値を確認する 2 つの異なる方法を示します ReadOnlyAttribute。 2 番目のコード フラグメントでは、 メソッドを呼び出します Equals 。 最後のコード フラグメントでは、 プロパティをIsReadOnly使用して値をチェックします。

// 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)["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.
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

でクラスをマークした場合はReadOnlyAttribute、次のコード例を使用して値をチェックします。

AttributeCollection^ attributes = TypeDescriptor::GetAttributes( MyProperty );
if ( attributes[ ReadOnlyAttribute::typeid ]->Equals( ReadOnlyAttribute::Yes ) )
{
   // Insert code here.
}
AttributeCollection attributes = 
   TypeDescriptor.GetAttributes(MyProperty);
if(attributes[typeof(ReadOnlyAttribute)].Equals(ReadOnlyAttribute.Yes)) {
   // Insert code here.
}
Dim attributes As AttributeCollection = TypeDescriptor.GetAttributes(MyProperty)
If attributes(GetType(ReadOnlyAttribute)).Equals(ReadOnlyAttribute.Yes) Then
    ' Insert code here.
End If

注釈

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

重要

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

詳細については、「属性」を参照してください。

コンストラクター

ReadOnlyAttribute(Boolean)

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

フィールド

Default

ReadOnlyAttribute の既定値である No を指定します。つまり、この属性が関連付けられたプロパティは読み書き可能です。 static フィールドは読み取り専用です。

No

この属性が関連付けられているプロパティが読み書き可能であり、変更できることを指定します。 static フィールドは読み取り専用です。

Yes

この属性が関連付けられているプロパティが読み取り専用であり、サーバー エクスプローラーで変更できないことを指定します。 static フィールドは読み取り専用です。

プロパティ

IsReadOnly

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

TypeId

派生クラスで実装されると、この Attribute の一意の識別子を取得します。

(継承元 Attribute)

メソッド

Equals(Object)

このインスタンスと指定したオブジェクトが等しいかどうかを示します。

GetHashCode()

このインスタンスのハッシュ コードを返します。

GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
IsDefaultAttribute()

この属性が既定値かどうかを判断します。

IsDefaultAttribute()

派生クラスでオーバーライドされるとき、このインスタンスの値が派生クラスの既定値であるかどうかを示します。

(継承元 Attribute)
Match(Object)

派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。

(継承元 Attribute)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

一連の名前を対応する一連のディスパッチ識別子に割り当てます。

(継承元 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

オブジェクトの型情報を取得します。この情報はインターフェイスの型情報の取得に使用できます。

(継承元 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。

(継承元 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

オブジェクトによって公開されたプロパティおよびメソッドへのアクセスを提供します。

(継承元 Attribute)

適用対象

こちらもご覧ください