System.Reflection.FieldAttributes Enumeration

Assembly: Mscorlib.dll
Namespace: System.Reflection
Summary
Specifies flags that describe the attributes of a field.
C# Syntax:
[Serializable]
public enum FieldAttributes
Remarks
FieldAttributes uses the value from FieldAccessMask to mask off only the parts of the attribute value that is the accessibility. For example, the following code determines if Attributes has the public bit set:

(Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public

To get the FieldAttributes, first get the class Type. From the Type, get the FieldInfo. From the FieldInfo, get the Attributes.

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

Example
In this example, three fields are built and the FieldAttributes value is displayed when it is exactly defined. A FieldAttributes may contain more than one attribute, such as both Public and Literal, as shown in the third field below.
 using System;
 using System.Reflection;

 //Make three fields
 //The first field is private
 public class Myfielda
 {
     private string field = "A private field";
     public string Field{
         get{return field;}
         set{if(field!=value) {field=value + "---";}}
     }
 }
 //The second field is public
 public class Myfieldb
 {
     public string field = "B public field";
     public string Field{
         get{return field;}
         set{if(field!=value) {field=value;}}
     }
 }
 //The third field is public and literal, which is not exactly defined.
 public class Myfieldc
 {
     public const string field = "C constant field";
     public string Field{
         get{return field;}
     }
 }
 public class Myfieldattributes
 {
     public static int Main()
     {
         Console.WriteLine ("\nReflection.FieldAttributes");
         Myfielda Myfielda = new Myfielda();
         Myfieldb Myfieldb = new Myfieldb();
         Myfieldc Myfieldc = new Myfieldc();
 
         //Get the Type and FieldInfo for each of the three fields
         Type MyTypea = Type.GetType("Myfielda");
         FieldInfo Myfieldinfoa = MyTypea.GetField("field",
             BindingFlags.NonPublic);
         Type MyTypeb = Type.GetType("Myfieldb");
         FieldInfo Myfieldinfob = MyTypeb.GetField("field",
             BindingFlags.NonPublic);
         Type MyTypec = Type.GetType("Myfieldc");
         FieldInfo Myfieldinfoc = MyTypec.GetField("field",
             BindingFlags.NonPublic);
 
         //For the first field;
         //Get and Display the Name, field, and attributes
         Console.Write ("\n{0} - ", MyTypea.FullName);
         Console.Write ("{0}; ", Myfieldinfoa.GetValue(Myfielda));
         FieldAttributes Myattributesa = Myfieldinfoa.Attributes;
 
         //If the FieldAttributes is exactly defined,
         // print it out, otherwise say it is not defined
         if (Enum.IsDefined(typeof(FieldAttributes),
             Myattributesa))
             Console.Write ("It has a {0} field attribute.",
                 Myattributesa.ToString());
         else
             Console.Write ("It is not exactly defined.");
 
         //For the second field;
         //Get and Display the Name, field, and attributes
         Console.Write ("\n{0} - ", MyTypeb.FullName);
         Console.Write ("{0}; ", Myfieldinfob.GetValue(Myfieldb));
         FieldAttributes Myattributesb = Myfieldinfob.Attributes;
 
         //If the FieldAttributes is exactly defined,
         // print it out, otherwise say it is not defined
         if (Enum.IsDefined(typeof(FieldAttributes),
             Myattributesb))
             Console.Write ("It has a {0} field attribute.",
                 Myattributesb.ToString());
         else
             Console.Write ("It is not exactly defined.");
 
         //For the third field;
         //Get and Display the Name, field, and attributes
         Console.Write ("\n{0} - ", MyTypec.FullName);
         Console.Write ("{0}; ", Myfieldinfoc.GetValue(Myfieldc));
         FieldAttributes Myattributesc = Myfieldinfoc.Attributes;
 
         //If the FieldAttributes is exactly defined,
         // print it out, otherwise say it is not defined
         if (Enum.IsDefined(typeof(FieldAttributes),
             Myattributesc))
             Console.Write ("It has a {0} field attribute.",
                 Myattributesc.ToString());
         else
             Console.Write ("It is not exactly defined.");
 
         return 0;
     }
 }

    

This code produces the following output:

Reflection.FieldAttributes

Myfielda - A private field; it has a Private field attribute.

Myfieldb - B public field; it has a Public field attribute.

Myfieldc - C constant field; it is not exactly defined.

See also:
System.Reflection Namespace

System.Reflection.FieldAttributes Member List:

Public Fields
Assembly Specifies that the field is accessible throughout the assembly.
FamANDAssem Specifies that the field is accessible only by subtypes in this assembly.
Family Specifies that the field is accessible only by type and subtypes.
FamORAssem Specifies that the field is accessible by subtypes anywhere, as well as throughout this assembly.
FieldAccessMask Specifies the access level of a given field.
HasDefault Specifies that the field has a default value.
HasFieldMarshal Specifies that the field has marshalling information.
HasFieldRVA Specifies that the field has a Relative Virtual Address (RVA). The RVA is the location of the method body in the current image, as an address relative to the start of the image file in which it is located.
InitOnly Specifies that the field is initialized only, and cannot be written after initialization.
Literal Specifies that the field's value is a compile-time (static or early bound) constant. No set accessor.
NotSerialized Specifies that the field does not have to be serialized when the type is remoted.
PinvokeImpl Reserved for future use.
Private Specifies that the field is accessible only by the parent type.
PrivateScope Specifies that the field cannot be referenced.
Public Specifies that the field is accessible by any member for whom this scope is visible.
ReservedMask Reserved.
RTSpecialName Specifies that the common language runtime (metadata internal APIs) should check the name encoding.
SpecialName Specifies a special method, with the name describing how the method is special.
Static Specifies that the field represents the defined type, or else it is per-instance.

Hierarchy:


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.