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

下一个代码示例演示如何为 检查 的值ReadOnlyAttributeMyProperty。 首先,代码获取 PropertyDescriptorCollection 包含 对象的所有属性的 。 接下来,它会索引到 中 PropertyDescriptorCollection 以获取 MyProperty。 然后,它将返回此属性的属性,并将其保存在 attributes 变量中。

该示例演示了检查 的值的 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 方法的成员。 没有此属性或标记为 ReadOnlyAttribute 设置为 false 的成员是可读/写的,可以更改它们。 默认值为 No

重要

PropertyDescriptor 在设计环境和运行时强制实施 ReadOnlyAttribute 。 将 属性ReadOnlyAttributetrue标记为 时,此属性的值设置为常量成员 Yes。 对于标记为 ReadOnlyAttribute 设置为 false的属性,值为 No。 因此,如果要在代码中检查此属性的值,必须将 特性指定为 ReadOnlyAttribute.YesReadOnlyAttribute.No

有关更多信息,请参阅特性

构造函数

ReadOnlyAttribute(Boolean)

初始化 ReadOnlyAttribute 类的新实例。

字段

Default

指定 ReadOnlyAttribute 的默认值,为 No(即此特性绑定到的属性是可读/写的)。 此 static 字段是只读的。

No

指定该特性绑定到的属性为读/写属性,可以修改。 此 static 字段是只读的。

Yes

指定此特性绑定到的属性为只读,并且不能在服务器资源管理器中修改。 此 static 字段是只读的。

属性

IsReadOnly

获取一个值,该值指示该特性绑定到的属性是否为只读属性。

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)

将一组名称映射为对应的一组调度标识符。

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。

(继承自 Attribute)

适用于

另请参阅