ReadOnlyAttribute Kelas

Definisi

Menentukan apakah properti yang terikat atribut ini bersifat baca-saja atau baca/tulis. Kelas ini tidak dapat diwariskan.

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
Warisan
ReadOnlyAttribute
Atribut

Contoh

Contoh kode berikut menandai properti sebagai baca-saja.

   [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

Contoh kode berikutnya menunjukkan cara memeriksa nilai ReadOnlyAttribute untuk MyProperty. Pertama, kode mendapatkan PropertyDescriptorCollection dengan semua properti untuk objek . Selanjutnya, ini mengindeks ke PropertyDescriptorCollection dalam untuk mendapatkan MyProperty. Kemudian mengembalikan atribut untuk properti ini dan menyimpannya dalam variabel atribut.

Contohnya menyajikan dua cara berbeda untuk memeriksa nilai ReadOnlyAttribute. Dalam fragmen kode kedua, contoh memanggil Equals metode . Dalam fragmen kode terakhir, contoh menggunakan IsReadOnly properti untuk memeriksa nilai.

// 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

Jika Anda menandai kelas dengan ReadOnlyAttribute, gunakan contoh kode berikut untuk memeriksa nilainya.

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

Keterangan

Anggota yang ditandai dengan diatur ke ReadOnlyAttributetrue atau yang tidak memiliki Set metode tidak dapat diubah. Anggota yang tidak memiliki atribut ini atau yang ditandai dengan ReadOnlyAttribute set ke false adalah baca/tulis, dan mereka dapat diubah. Default adalah No.

Penting

Kelas memberlakukan PropertyDescriptorReadOnlyAttribute di lingkungan desain dan pada waktu proses. Saat Anda menandai properti dengan diatur ReadOnlyAttribute ke true, nilai atribut ini diatur ke anggota Yeskonstanta . Untuk properti yang ditandai dengan ReadOnlyAttribute diatur ke false, nilainya adalah No. Oleh karena itu, ketika Anda ingin memeriksa nilai atribut ini dalam kode Anda, Anda harus menentukan atribut sebagai ReadOnlyAttribute.Yes atau ReadOnlyAttribute.No.

Untuk informasi selengkapnya, lihat Atribut.

Konstruktor

ReadOnlyAttribute(Boolean)

Menginisialisasi instans baru kelas ReadOnlyAttribute.

Bidang

Default

Menentukan nilai default untuk ReadOnlyAttribute, yaitu No (yaitu, properti yang terikat atribut ini adalah baca/tulis). Bidang ini static bersifat baca-saja.

No

Menentukan bahwa properti yang terikat dengan atribut ini adalah baca/tulis dan dapat dimodifikasi. Bidang ini static bersifat baca-saja.

Yes

Menentukan bahwa properti yang terikat atribut ini bersifat baca-saja dan tidak dapat dimodifikasi di penjelajah server. Bidang ini static bersifat baca-saja.

Properti

IsReadOnly

Mendapatkan nilai yang menunjukkan apakah properti yang terikat atribut ini bersifat baca-saja.

TypeId

Ketika diimplementasikan di kelas turunan, mendapatkan pengidentifikasi unik untuk ini Attribute.

(Diperoleh dari Attribute)

Metode

Equals(Object)

Menunjukkan apakah instans ini dan objek tertentu sama.

GetHashCode()

Mengembalikan kode hash untuk instans ini.

GetType()

Mendapatkan instans Type saat ini.

(Diperoleh dari Object)
IsDefaultAttribute()

Menentukan apakah atribut ini adalah default.

IsDefaultAttribute()

Ketika ditimpa di kelas turunan, menunjukkan apakah nilai instans ini adalah nilai default untuk kelas turunan.

(Diperoleh dari Attribute)
Match(Object)

Saat ditimpa di kelas turunan, mengembalikan nilai yang menunjukkan apakah instans ini sama dengan objek tertentu.

(Diperoleh dari Attribute)
MemberwiseClone()

Membuat salinan dangkal dari yang saat ini Object.

(Diperoleh dari Object)
ToString()

Mengembalikan string yang mewakili objek saat ini.

(Diperoleh dari Object)

Implementasi Antarmuka Eksplisit

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

Memetakan sekumpulan nama ke sekumpulan pengidentifikasi pengiriman yang sesuai.

(Diperoleh dari Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Mengambil informasi jenis untuk objek, yang dapat digunakan untuk mendapatkan informasi jenis untuk antarmuka.

(Diperoleh dari Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Mengambil jumlah antarmuka informasi jenis yang disediakan objek (baik 0 atau 1).

(Diperoleh dari Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Menyediakan akses ke properti dan metode yang diekspos oleh objek.

(Diperoleh dari Attribute)

Berlaku untuk

Lihat juga