共用方式為


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 =>
        // Insert code here.
        0;
Public ReadOnly Property MyProperty() As Integer
    Get
        ' Insert code here.
        Return 0
    End Get
End Property

下一個程式碼範例展示了如何檢查 的值ReadOnlyAttributeMyProperty 首先,程式碼會取得包含所有物件屬性的 a 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

備註

標示 ReadOnlyAttribute 為 的 true 成員或沒有 Set 方法的成員無法更改。 沒有此屬性或以 ReadOnlyAttributefalse 標記的成員會被讀寫,且可以更改。 預設值為 No

這很重要

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

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

建構函式

名稱 Description
ReadOnlyAttribute(Boolean)

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

欄位

名稱 Description
Default

指定 的 ReadOnlyAttribute預設值,即 No 該屬性綁定的屬性為讀寫。 此 static 欄位為唯讀。

No

指定此屬性綁定的屬性為讀寫,且可修改。 此 static 欄位為唯讀。

Yes

指定此屬性綁定的屬性為唯讀,且無法在伺服器檔案總管中修改。 此 static 欄位為唯讀。

屬性

名稱 Description
IsReadOnly

會得到一個值,表示該屬性所綁定的屬性是否為唯讀。

TypeId

在衍生類別中實作時,取得這個 Attribute的唯一標識碼。

(繼承來源 Attribute)

方法

名稱 Description
Equals(Object)

指出這個實例和指定的物件是否相等。

GetHashCode()

傳回這個實例的哈希碼。

GetType()

取得目前實例的 Type

(繼承來源 Object)
IsDefaultAttribute()

判斷此屬性是否為預設值。

Match(Object)

在衍生類別中覆寫時,傳回值,指出這個實例是否等於指定的物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

明確介面實作

名稱 Description
_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)

適用於

另請參閱