Freigeben über


ReadOnlyAttribute-Klasse

Gibt an, ob es sich bei der Eigenschaft, an die dieses Attribut gebunden ist, zur Entwurfszeit um eine schreibgeschützte Eigenschaft oder um eine Lese-/Schreib-Eigenschaft handelt. Diese Klasse kann nicht geerbt werden.

Namespace: System.ComponentModel
Assembly: System (in system.dll)

Syntax

'Declaration
<AttributeUsageAttribute(AttributeTargets.All)> _
Public NotInheritable Class ReadOnlyAttribute
    Inherits Attribute
'Usage
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

Hinweise

Member, die mit ReadOnlyAttribute mit dem Wert true markiert sind oder nicht über eine Set-Methode verfügen, können nicht geändert werden. Member, die nicht über dieses Attribut verfügen oder mit ReadOnlyAttribute mit dem Wert false markiert sind, sind Lese-/Schreibmember und können geändert werden. Der Standardwert ist No.

Wichtig

Die PropertyDescriptor-Klasse erzwingt das ReadOnlyAttribute in der Entwurfsumgebung und zur Laufzeit. Wenn Sie eine Eigenschaft mit ReadOnlyAttribute mit dem Wert true markieren, wird der Wert dieses Attributs auf den konstanten Member Yes festgelegt. Für eine mit ReadOnlyAttribute mit dem Wert false markierte Eigenschaft ist der Wert No. Wenn Sie den Wert dieses Attributs im Code überprüfen möchten, müssen Sie deshalb das Attribut als ReadOnlyAttribute.Yes oder ReadOnlyAttribute.No angeben.

Weitere Informationen finden Sie unter Übersicht über Attribute und Erweitern von Metadaten mithilfe von Attributen.

Beispiel

Im folgenden Codebeispiel wird eine Eigenschaft als schreibgeschützt markiert.

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

Im folgenden Beispiel wird veranschaulicht, wie der Wert von ReadOnlyAttribute für MyProperty überprüft wird. Zunächst wird im Code eine PropertyDescriptorCollection mit allen Eigenschaften für das Objekt abgerufen. Anschließend wird MyProperty über den Index in PropertyDescriptorCollection abgerufen. Die Attribute für diese Eigenschaft werden zurückgegeben und in der Variablen attributes gespeichert.

Im Beispiel werden zwei verschiedene Möglichkeiten zum Überprüfen des Werts von ReadOnlyAttribute gezeigt. Im zweiten Codefragment wird die Equals-Methode aufgerufen. Im letzten Codefragment wird der Wert anhand der IsReadOnly-Eigenschaft überprüft.

' 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.
}

Wenn Sie eine Klasse mit ReadOnlyAttribute markiert haben, überprüfen Sie den Wert mit folgendem Code.

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.
}

Vererbungshierarchie

System.Object
   System.Attribute
    System.ComponentModel.ReadOnlyAttribute

Threadsicherheit

Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Plattformen

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

ReadOnlyAttribute-Member
System.ComponentModel-Namespace
Attribute
PropertyDescriptor-Klasse
AttributeCollection-Klasse
PropertyDescriptorCollection-Klasse