ReadOnlyAttribute 類別

定義

指定這個屬性 (Attribute) 繫結的屬性 (Property) 是唯讀的或是讀取/寫入的。 此類別無法獲得繼承。

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

下一個程式碼範例示範如何檢查 ReadOnlyAttribute 的值 MyProperty 。 首先,程式碼會取得 PropertyDescriptorCollection 具有 物件之所有屬性的 。 接下來,它會編制索引至 PropertyDescriptorCollection 以取得 MyProperty 。 然後它會傳回此屬性的屬性,並將其儲存在屬性變數中。

此範例提供兩種檢查 值 ReadOnlyAttribute 的不同方式。 在第二個程式碼片段中,此範例會呼叫 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

備註

ReadOnlyAttributetrue 標示為 或沒有 Set 方法的成員無法變更。 沒有這個屬性或標示 ReadOnlyAttributefalse 的成員是可讀寫的,而且可以變更。 預設為 No

重要

類別 PropertyDescriptor 會在 ReadOnlyAttribute 設計環境中強制執行 ,並在執行時間強制執行 。 當您將 屬性標示 ReadOnlyAttributetrue 時,這個屬性的值會設定為常數成員 Yes 。 對於標示為 的屬性, ReadOnlyAttributefalse 此值為 No 。 因此,當您想要在程式碼中檢查此屬性的值時,您必須將 屬性指定為 ReadOnlyAttribute.YesReadOnlyAttribute.No

如需詳細資訊,請參閱屬性

建構函式

ReadOnlyAttribute(Boolean)

初始化 ReadOnlyAttribute 類別的新執行個體。

欄位

Default

指定 ReadOnlyAttribute 的預設值,即 No (換句話說,此屬性 (Attribute) 所繫結的屬性 (Property) 是可以讀取和寫入的)。 這個 static 欄位是唯讀的。

No

指定這個屬性 (Attribute) 繫結的屬性 (Property) 是可讀取/寫入的,並可進行修改。 這個 static 欄位是唯讀的。

Yes

指定這個屬性 (Attribute) 繫結的屬性 (Property) 是唯讀的,並無法在伺服器總管中進行修改。 這個 static 欄位是唯讀的。

屬性

IsReadOnly

取得值,表示這個屬性 (Attribute) 繫結的內容 (Property) 是否為唯讀。

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)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱