Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

PropertyAttributes Enumeration

Defines the attributes that can be associated with a property. These attribute values are defined in corhdr.h.

This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.

Namespace: System.Reflection
Assembly: mscorlib (in mscorlib.dll)

[SerializableAttribute] 
[FlagsAttribute] 
[ComVisibleAttribute(true)] 
public enum PropertyAttributes
/** @attribute SerializableAttribute() */ 
/** @attribute FlagsAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public enum PropertyAttributes
SerializableAttribute 
FlagsAttribute 
ComVisibleAttribute(true) 
public enum PropertyAttributes
 Member nameDescription
Supported by the .NET Compact FrameworkHasDefaultSpecifies that the property has a default value. 
Supported by the .NET Compact FrameworkNoneSpecifies that no attributes are associated with a property. 
Supported by the .NET Compact FrameworkReserved2Reserved. 
Supported by the .NET Compact FrameworkReserved3Reserved. 
Supported by the .NET Compact FrameworkReserved4Reserved. 
Supported by the .NET Compact FrameworkReservedMaskSpecifies a flag reserved for runtime use only. 
Supported by the .NET Compact FrameworkRTSpecialNameSpecifies that the metadata internal APIs check the name encoding. 
Supported by the .NET Compact FrameworkSpecialNameSpecifies that the property is special, with the name describing how the property is special. 

To get the PropertyAttributes, first get the class Type. From the Type, get the PropertyInfo. From the PropertyInfo, get the Attributes.

The enumerated value is a number representing the bitwise OR of the attributes implemented on the method.

The following example builds three properties and displays the PropertyAttributes enumerated value. Note that the read-only property has no setter and thus cannot be changed by.Caption = statement.

using System;
using System.Reflection;
 
 // Define three properties: one read-write, one default,
 // and one read only. 
public class Aproperty  
    // Define a read-write property.
{
    private string caption = "A Default caption";
    public string Caption
    {
        get{return caption;}
        set
        {
            if (caption != value){caption = value;}
        }
    }
}
public class Bproperty  
    // Define a default property.
{
    private string caption  = "B Default caption";
    public string this [int index]
    {
        get {return "1";}
    }
    public string Caption
    {
  
        get{return caption;}
        set
        {
            if (caption != value){caption = value;}
        }
    }
}
public class Cproperty  
    // Define a read-only property.
{
    private string caption = "C Default caption";
    public string Caption
    {
        get{return caption;}
        // No setting is allowed, because this is a read-only property.
    }
}
  
class propertyattributesenum
{
    public static int Main(string[] args)
    {
        Console.WriteLine("\nReflection.PropertyAttributes");
  
        // Determine whether a property exists, and change its value.
        Aproperty Mypropertya = new Aproperty();
        Bproperty Mypropertyb = new Bproperty();
        Cproperty Mypropertyc = new Cproperty();
  
      
        Console.Write("\n1. Mypropertya.Caption = " + Mypropertya.Caption );
      
        Console.Write("\n1. Mypropertyb.Caption = " + Mypropertyb.Caption );
      
        Console.Write("\n1. Mypropertyc.Caption = " + Mypropertyc.Caption );
  
        // Only Mypropertya can be changed, as Mypropertyb is read-only.
        Mypropertya.Caption = "A- This is changed.";
        Mypropertyb.Caption = "B- This is changed.";
        // Note that Mypropertyc is not changed because it is read only
  
        Console.Write("\n\n2. Mypropertya.Caption = " + Mypropertya.Caption );
  
        Console.Write("\n2.Mypropertyb.Caption = " + Mypropertyb.Caption );
 
        Console.Write("\n2. Mypropertyc.Caption = " + Mypropertyc.Caption );
  
        // Get the PropertyAttributes enumeration of the property.
        // Get the type.
        Type MyTypea = Type.GetType("Aproperty");
        Type MyTypeb = Type.GetType("Bproperty");
        Type MyTypec = Type.GetType("Cproperty");
  
        // Get the property attributes.
        PropertyInfo Mypropertyinfoa = MyTypea.GetProperty("Caption");
        PropertyAttributes Myattributesa = Mypropertyinfoa.Attributes;
        PropertyInfo Mypropertyinfob = MyTypeb.GetProperty("Item");
        PropertyAttributes Myattributesb = Mypropertyinfob.Attributes;
        PropertyInfo Mypropertyinfoc = MyTypec.GetProperty("Caption");
        PropertyAttributes Myattributesc = Mypropertyinfoc.Attributes;
  
        // Display the property attributes value.
      
        Console.Write("\n\na- " + Myattributesa.ToString());
  
        Console.Write("\nb-" + Myattributesb.ToString());
      
        Console.Write("\nc- " + Myattributesc.ToString());
        return 0;
    }
}

import System.*;
import System.Reflection.*;

// Define three properties: one read-write, one default,
// and one read only. 
public class AProperty
{
	// Define a read-write property.
	private String caption = "A Default caption";

	/** @property 
	 */
	public String get_Caption()
	{
		return caption ;
	} //get_Caption

	/** @property 
	 */
	public void set_Caption(String value)
	{
		if (caption != value) {
			caption = value;
		}
	} //set_Caption
} //AProperty

public class BProperty
{
	// Define a default property.
	private String caption = "B Default caption";

	/** @property 
	 */
	public String get_Item(int index)
	{
		return "1" ;
	} //get_Item

	/** @property 
	 */
	public String get_Caption()
	{
		return caption ;
	} //get_Caption

	/** @property 
	 */
    public void set_Caption (String value)
	{
		if (caption != value) {
			caption = value;
		}
	} //set_Caption
} //BProperty

public class CProperty
{
	// Define a read-only property.
	private String caption = "C Default caption";

	/** @property 
	 */
	public String get_Caption()
	{
		return caption ;
		// No setting is allowed, because this is a read-only property.
	} //get_Caption
} //CProperty

class PropertyAttributesEnum
{   
	public static void main(String[] args)
	{
		Console.WriteLine("\nReflection.PropertyAttributes");

		// Determine whether a property exists, and change its value.
		AProperty myPropertyA =  new AProperty();
		BProperty myPropertyB =  new BProperty();
		CProperty myPropertyC =  new CProperty();

		Console.Write(("\n1. Mypropertya.Caption = " 
			+ myPropertyA.get_Caption()));
		Console.Write(("\n1. Mypropertyb.Caption = " 
			+ myPropertyB.get_Caption()));
		Console.Write(("\n1. Mypropertyc.Caption = " 
			+ myPropertyC.get_Caption()));

		// Only myPropertyA can be changed, as myPropertyB is read-only.
		myPropertyA.set_Caption("A- This is changed.");
		myPropertyB.set_Caption("B- This is changed.");
		// Note that myPropertyC is not changed because it is read only
		Console.Write(("\n\n2. Mypropertya.Caption = " 
			+ myPropertyA.get_Caption()));
		Console.Write(("\n2.Mypropertyb.Caption = " 
			+ myPropertyB.get_Caption()));
		Console.Write(("\n2. Mypropertyc.Caption = " 
			+ myPropertyC.get_Caption()));

		// Get the PropertyAttributes enumeration of the property.
		// Get the type.
		Type myTypeA = Type.GetType("AProperty");
		Type myTypeB = Type.GetType("BProperty");
		Type myTypeC = Type.GetType("CProperty");

		// Get the property attributes.
		PropertyInfo myPropertyInfoA = myTypeA.GetProperty("Caption");
		PropertyAttributes myAttributesA = myPropertyInfoA.get_Attributes();
		PropertyInfo myPropertyInfoB = myTypeB.GetProperty("Item");
		PropertyAttributes myAttributesB = myPropertyInfoB.get_Attributes();
		PropertyInfo myPropertyInfoC = myTypeC.GetProperty("Caption");
		PropertyAttributes myAttributesC = myPropertyInfoC.get_Attributes();

		// Display the property attributes value.
		Console.Write(("\n\nA- " + myAttributesA.ToString()));
		Console.Write(("\nB-" + myAttributesB.ToString()));
		Console.Write(("\nC- " + myAttributesC.ToString()));
	} //main
} //PropertyAttributesEnum

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.