PropertyAttributes Enumeration
.NET Framework 3.0
Defines the attributes that can be associated with a property. These attribute values are defined in corhdr.h.
Assembly: mscorlib (in mscorlib.dll)
This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.
Namespace: System.ReflectionAssembly: mscorlib (in mscorlib.dll)
'Declaration <SerializableAttribute> _ <FlagsAttribute> _ <ComVisibleAttribute(True)> _ Public Enumeration PropertyAttributes 'Usage Dim instance As PropertyAttributes
/** @attribute SerializableAttribute() */ /** @attribute FlagsAttribute() */ /** @attribute ComVisibleAttribute(true) */ public enum PropertyAttributes
SerializableAttribute FlagsAttribute ComVisibleAttribute(true) public enum PropertyAttributes
| Member name | Description | |
|---|---|---|
![]() | HasDefault | Specifies that the property has a default value. |
![]() | None | Specifies that no attributes are associated with a property. |
![]() | Reserved2 | Reserved. |
![]() | Reserved3 | Reserved. |
![]() | Reserved4 | Reserved. |
![]() | ReservedMask | Specifies a flag reserved for runtime use only. |
![]() | RTSpecialName | Specifies that the metadata internal APIs check the name encoding. |
![]() | SpecialName | Specifies that the property is special, with the name describing how the property is special. |
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.
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
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 Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.Community Additions
ADD
Show:
