PropertyAttributes Enum
System.Reflection
Specifies attributes that can be set for properties. These attributes provide metadata about the property.
Syntax
public enum PropertyAttributes
Description
The PropertyAttributes
enumeration is used by the reflection infrastructure to describe the characteristics of properties in a type. Each member of the enumeration represents a specific attribute that can be associated with a property, such as whether it is static, abstract, or has specific access modifiers. These attributes are stored as metadata and can be accessed programmatically through the PropertyInfo
class.
Members
Member | Description |
---|---|
None |
No attributes are set for the property. |
FixedSize |
The property has a fixed size. |
HasDefault |
The property has a default value. |
IsSpecialName |
The property has a special name, such as an accessor for a built-in operation. |
RTSpecialName |
The property has a special name, but it is managed by the runtime. |
HasField |
The property has an associated backing field. |
SpecialAddressing |
The property is used for special addressing. |
ReservedMask |
A mask for reserved bits. |
Remarks
The PropertyAttributes
enumeration is a bit flag enumeration, meaning that multiple attributes can be combined using the bitwise OR operator (|
). For example, a property could have both HasDefault
and IsSpecialName
attributes set.
When you retrieve a PropertyInfo
object for a property, you can access its attributes using the Attributes
property, which returns a value of type PropertyAttributes
.
Example
using System;
using System.Reflection;
public class MyClass
{
public int MyProperty { get; set; } = 42;
public string SpecialProperty { get; } // Example of a special name property (e.g., indexer)
}
public class Example
{
public static void Main(string[] args)
{
Type myClassType = typeof(MyClass);
PropertyInfo[] properties = myClassType.GetProperties();
foreach (PropertyInfo property in properties)
{
Console.WriteLine($"Property: {property.Name}");
Console.WriteLine($" Attributes: {property.Attributes}");
if ((property.Attributes & PropertyAttributes.HasDefault) != 0)
{
Console.WriteLine(" -> Has Default Value");
}
if ((property.Attributes & PropertyAttributes.IsSpecialName) != 0)
{
Console.WriteLine(" -> Is Special Name");
}
Console.WriteLine();
}
}
}