Export (0) Print
Expand All
Expand Minimize

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.

[Visual Basic]
<Flags>
<Serializable>
Public Enum PropertyAttributes
[C#]
[Flags]
[Serializable]
public enum PropertyAttributes
[C++]
[Flags]
[Serializable]
__value public enum PropertyAttributes
[JScript]
public
   Flags
 Serializable
enum PropertyAttributes

Remarks

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.

Members

Member name Description Value
HasDefault

Supported by the .NET Compact Framework.

Specifies that the property has a default value. 4096
None

Supported by the .NET Compact Framework.

Specifies that no attributes are associated with a property. 0
Reserved2

Supported by the .NET Compact Framework.

Reserved. 8192
Reserved3

Supported by the .NET Compact Framework.

Reserved. 16384
Reserved4

Supported by the .NET Compact Framework.

Reserved. 32768
ReservedMask

Supported by the .NET Compact Framework.

Specifies a flag reserved for runtime use only. 62464
RTSpecialName

Supported by the .NET Compact Framework.

Specifies that the metadata internal APIs check the name encoding. 1024
SpecialName

Supported by the .NET Compact Framework.

Specifies that the property is special, with the name describing how the property is special. 512

Example

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

' Make three properties, one read-write, one default,
' and one read-only. 
Public Class Aproperty
    ' Define a read-write property.
    Private myCaption As String = "A Default caption"

    Public Property Caption() As String
        Get
            Return myCaption
        End Get
        Set(ByVal Value As String)
            If myCaption <> value Then
                myCaption = value
            End If
        End Set
    End Property
End Class

Public Class Bproperty
    ' Define a default property.
    Private myCaption As String = "B Default caption"

    Default Public ReadOnly Property Item(ByVal index As Integer) As String
        Get
            Return "1"
        End Get
    End Property

    Public Property Caption() As String

        Get
            Return myCaption
        End Get
        Set(ByVal Value As String)
            If myCaption <> value Then
                myCaption = value
            End If
        End Set
    End Property
End Class

Public Class Cproperty
    ' Define a read-only property.
    Private myCaption As String = "C Default caption"

    Public ReadOnly Property Caption() As String
        Get
            Return myCaption
        End Get
        'No setting is allowed because this property is read-only.
    End Property
End Class


Class propertyattributesenum

    Public Shared Function Main() As Integer
        Console.WriteLine(ControlChars.CrLf & "Reflection.PropertyAttributes")

        ' Determine whether a property exists, and change its value.
        Dim Mypropertya As New Aproperty()
        Dim Mypropertyb As New Bproperty()
        Dim Mypropertyc As New Cproperty()

        Console.Write(ControlChars.CrLf & "1. Mypropertya.Caption = " & _
           Mypropertya.Caption)

        Console.Write(ControlChars.CrLf & "1. Mypropertyb.Caption = " & _
           Mypropertyb.Caption)

        Console.Write(ControlChars.CrLf & "1. Mypropertyc.Caption = " & _
           Mypropertyc.Caption)

        ' Only Mypropertya can be changed because 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(ControlChars.CrLf & ControlChars.CrLf & _
           "2. Mypropertya.Caption = " & Mypropertya.Caption)

        Console.Write(ControlChars.CrLf & "2.Mypropertyb.Caption = " & _
           Mypropertyb.Caption)

        Console.Write(ControlChars.CrLf + "2. Mypropertyc.Caption = " & _
           Mypropertyc.Caption)

        ' Get the PropertyAttributes Enumeration of the property.
        ' Get the type.
        Dim MyTypea As Type = Type.GetType("Aproperty")
        Dim MyTypeb As Type = Type.GetType("Bproperty")
        Dim MyTypec As Type = Type.GetType("Cproperty")

        ' Get the property attributes.
        Dim Mypropertyinfoa As PropertyInfo = MyTypea.GetProperty("Caption")
        Dim Myattributesa As PropertyAttributes = Mypropertyinfoa.Attributes
        Dim Mypropertyinfob As PropertyInfo = MyTypeb.GetProperty("Item")
        Dim Myattributesb As PropertyAttributes = Mypropertyinfob.Attributes
        Dim Mypropertyinfoc As PropertyInfo = MyTypec.GetProperty("Caption")
        Dim Myattributesc As PropertyAttributes = Mypropertyinfoc.Attributes

        ' Display the property attributes value.
        Console.Write(ControlChars.CrLf & ControlChars.CrLf & "a- " & _
           Myattributesa.ToString())

        Console.Write(ControlChars.CrLf & "b-" & Myattributesb.ToString())

        Console.Write(ControlChars.CrLf & "c- " & Myattributesc.ToString())
        Return 0
    End Function
End Class

[C#] 
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;
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;

// Define three properties: one read-write, one default,
// and one read only. 
public __gc class Aproperty  
// Define a read-write property.
{
private:
    String* caption;
public:
    Aproperty() : caption(S"A Default caption") {}
    __property String* get_Caption() {
        return caption;
    }
    __property void set_Caption(String* value) {
        if (caption != value){
            caption = value;
        }
    }
};

public __gc class Bproperty  
// Define a default property.
{
private:
    String* caption;
public:
    Bproperty() : caption(S"B Default caption") {}
    __property String* get_Item (int index) {
        return S"1";
    }
    __property String* get_Caption() {
        return caption;
    }
    __property void set_Caption(String* value) {
        if (caption != value){
            caption = value;
        }
    }
};

public __gc class Cproperty  
// Define a read-only property.
{
private:
    String* caption;
public:
    Cproperty() : caption(S"C Default caption") {}
    __property String* get_Caption() {
        return caption;
    }
};

int main()
{
    Console::WriteLine(S"\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(S"\n1. Mypropertya->Caption = {0}", Mypropertya->Caption );

    Console::Write(S"\n1. Mypropertyb->Caption = {0}", Mypropertyb->Caption );

    Console::Write(S"\n1. Mypropertyc->Caption = {0}", Mypropertyc->Caption );

    // Only Mypropertya can be changed, as Mypropertyb is read-only.
    Mypropertya->Caption = S"A- This is changed.";
    Mypropertyb->Caption = S"B- This is changed.";
    // Note that Mypropertyc is not changed because it is read only

    Console::Write(S"\n\n2. Mypropertya->Caption = {0}", Mypropertya->Caption );

    Console::Write(S"\n2.Mypropertyb->Caption = {0}", Mypropertyb->Caption );

    Console::Write(S"\n2. Mypropertyc->Caption = {0}", Mypropertyc->Caption );

    // Get the PropertyAttributes enumeration of the property.
    // Get the type.
    Type* MyTypea = Type::GetType(S"Aproperty");
    Type* MyTypeb = Type::GetType(S"Bproperty");
    Type* MyTypec = Type::GetType(S"Cproperty");

    // Get the property attributes.
    PropertyInfo* Mypropertyinfoa = MyTypea->GetProperty(S"Caption");
    PropertyAttributes Myattributesa = Mypropertyinfoa->Attributes;
    PropertyInfo* Mypropertyinfob = MyTypeb->GetProperty(S"Item");
    PropertyAttributes Myattributesb = Mypropertyinfob->Attributes;
    PropertyInfo* Mypropertyinfoc = MyTypec->GetProperty(S"Caption");
    PropertyAttributes Myattributesc = Mypropertyinfoc->Attributes;

    // Display the property attributes value.

    Console::Write(S"\n\na- {0}", __box(Myattributesa));

    Console::Write(S"\nb-{0}", __box(Myattributesb));

    Console::Write(S"\nc- {0}", __box(Myattributesc));
    return 0;
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Reflection

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

System.Reflection Namespace

Show:
© 2014 Microsoft