ReadOnlyAttribute 类

指定该属性 (Attribute) 所绑定到的属性 (Property) 在设计时是只读属性 (Property) 还是读/写属性 (Property)。无法继承此类

**命名空间:**System.ComponentModel
**程序集:**System(在 system.dll 中)

语法

声明
<AttributeUsageAttribute(AttributeTargets.All)> _
Public NotInheritable Class ReadOnlyAttribute
    Inherits Attribute
用法
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

备注

不能更改 ReadOnlyAttribute 设置为 true 或者不具有 Set 方法的成员。不具有此属性的成员或者 ReadOnlyAttribute 设置为 false 的成员是可读/写的,可以更改。默认为 No

Note重要事项:

PropertyDescriptor 类在设计环境中和运行时强制 ReadOnlyAttribute。当使用设置为 trueReadOnlyAttribute 标记某个属性 (Property) 时,此属性 (Attribute) 的值会被设置为常数成员 Yes。对于通过将 ReadOnlyAttribute 设置为 false 来标记的属性,则此值为 No。因此,若要检查代码中此属性的值,必须将该属性指定为 ReadOnlyAttribute.YesReadOnlyAttribute.No

有关更多信息,请参见 属性 (Attribute) 概述利用属性扩展元数据

示例

下面的代码示例将属性标记为只读。

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

下一个代码示例演示如何检查 MyPropertyReadOnlyAttribute 的值。首先,代码获取具有该对象的所有属性的 PropertyDescriptorCollection。接着,它按索引检索 PropertyDescriptorCollection 以获取 MyProperty。然后它返回该属性 (Property) 的属性 (Attribute),并将它们保存到属性 (Attribute) 变量中。

该示例提供了两种不同的方法来检查 ReadOnlyAttribute 的值。在第二个代码段中,该示例调用 Equals 方法。在最后一个代码段中,该示例使用 IsReadOnly 属性 (Property) 检查该值。

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

如果将类标记为 ReadOnlyAttribute,则可使用下面的代码示例检查该值。

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

继承层次结构

System.Object
   System.Attribute
    System.ComponentModel.ReadOnlyAttribute

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

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 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

ReadOnlyAttribute 成员
System.ComponentModel 命名空间
Attribute
PropertyDescriptor 类
AttributeCollection 类
PropertyDescriptorCollection 类