다음을 통해 공유


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

다음 코드 예제에서는 forMyPropertyReadOnlyAttribute 값을 확인하는 방법을 보여줍니다. 먼저 코드는 개체에 대한 모든 속성을 가진 a PropertyDescriptorCollection 를 가져옵니다. 다음으로, 가져올 MyProperty인덱싱합니다PropertyDescriptorCollection. 그런 다음, 이 속성에 대한 특성을 반환하고 특성 변수에 저장합니다.

이 예제에서는 두 가지 방법으로 값을 확인합니다 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

설명

집합 true 으로 ReadOnlyAttribute 표시되었거나 메서드가 없는 멤버는 Set 변경할 수 없습니다. 이 특성이 없거나 집합 false 으로 ReadOnlyAttribute 표시된 멤버는 읽기/쓰기가 가능하며 변경할 수 있습니다. 기본값은 No입니다.

중요합니다

클래스는 PropertyDescriptor 디자인 환경 및 런타임에 적용합니다 ReadOnlyAttribute . 설정된 true속성을 ReadOnlyAttribute 표시하면 이 특성의 값이 상수 멤버Yes로 설정됩니다. 집합false으로 표시된 속성의 ReadOnlyAttribute 경우 값은 .입니다No. 따라서 코드에서 이 특성의 값을 확인하려면 특성을 다음과 같이 ReadOnlyAttribute.YesReadOnlyAttribute.No지정해야 합니다.

자세한 내용은 특성을 참조하세요.

생성자

Name Description
ReadOnlyAttribute(Boolean)

ReadOnlyAttribute 클래스의 새 인스턴스를 초기화합니다.

필드

Name Description
Default

(즉, 이 특성이 바인딩된 No 속성이 읽기/쓰기인 경우)에 대한 ReadOnlyAttribute기본값을 지정합니다. 이 static 필드는 읽기 전용입니다.

No

이 특성이 바인딩된 속성이 읽기/쓰기이며 수정할 수 있도록 지정합니다. 이 static 필드는 읽기 전용입니다.

Yes

이 특성이 바인딩된 속성이 읽기 전용이며 서버 탐색기에서 수정할 수 없도록 지정합니다. 이 static 필드는 읽기 전용입니다.

속성

Name Description
IsReadOnly

이 특성이 바인딩된 속성이 읽기 전용인지 여부를 나타내는 값을 가져옵니다.

TypeId

파생 클래스에서 구현되는 경우 이 Attribute대한 고유 식별자를 가져옵니다.

(다음에서 상속됨 Attribute)

메서드

Name Description
Equals(Object)

이 인스턴스와 지정된 개체가 같은지 여부를 나타냅니다.

GetHashCode()

이 인스턴스의 해시 코드를 반환합니다.

GetType()

현재 인스턴스의 Type 가져옵니다.

(다음에서 상속됨 Object)
IsDefaultAttribute()

이 특성이 기본값인지 여부를 확인합니다.

Match(Object)

파생 클래스에서 재정의되는 경우 이 인스턴스가 지정된 개체와 같은지 여부를 나타내는 값을 반환합니다.

(다음에서 상속됨 Attribute)
MemberwiseClone()

현재 Object단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

명시적 인터페이스 구현

Name Description
_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)

적용 대상

추가 정보