ReadOnlyAttribute.IsReadOnly 属性

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

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

语法

声明
Public ReadOnly Property IsReadOnly As Boolean
用法
Dim instance As ReadOnlyAttribute
Dim value As Boolean

value = instance.IsReadOnly
public bool IsReadOnly { get; }
public:
property bool IsReadOnly {
    bool get ();
}
/** @property */
public boolean get_IsReadOnly ()
public function get IsReadOnly () : boolean

属性值

如果该属性 (Attribute) 所绑定到的属性 (Property) 为只读属性 (Property),则为 true;如果该属性 (Property) 为读/写属性 (Property),则为 false

示例

下面的代码示例检查 MyProperty 是否为只读。首先,通过执行以下操作,此代码获取 MyProperty 的属性:

  • 检索具有该对象的所有属性的 PropertyDescriptorCollection

  • PropertyDescriptorCollection 进行索引,以获取 MyProperty

  • 将该属性 (Property) 的属性 (Attribute) 保存在属性 (Attribute) 变量中。

然后,此代码将 myAttribute 设置为 AttributeCollectionReadOnlyAttribute 的值,并检查该属性是否为只读属性。

' Gets the attributes for the property.
Dim attributes As AttributeCollection = _
   TypeDescriptor.GetProperties(Me)("MyProperty").Attributes
       
' Checks 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 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 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 property is read-only.
ReadOnlyAttribute myAttribute = ((ReadOnlyAttribute)
    (attributes.get_Item(ReadOnlyAttribute.class.ToType())));

if (myAttribute.get_IsReadOnly()) {
    // Insert code here.
}

平台

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 类
ReadOnlyAttribute 成员
System.ComponentModel 命名空间
ReadOnlyAttribute 类
Attribute
PropertyDescriptor 类
AttributeCollection 类
PropertyDescriptorCollection 类