Comprises flags that specify the attributes of a field.
[Visual Basic] Public Enum FieldAttributes [C#] public enum FieldAttributes [C++] public enum FieldAttributes
[JScript] In JScript, you can use the enumerations in the NGWS frameworks, but you cannot define your own.
FieldAttributes uses this value from the FieldAccessMask property to mask off only the parts of the attribute value that is the accessibility. For example, the following code snippet determines if Attributes has the public bit set:
(Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public
In the example shown below, three fields are build and the FieldAttributes value is displayed, when it is exactly defined. A FieldAttributes may contain more than one attributes, such as Public and Literal, as shown in the third field below.
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. Us the following mask values to retrieve accessible information
FieldAccessMask = 0x0007,
PrivateScope = 0x0000
Private = 0x0001
FamANDAssem = 0x0002
Assembly = 0x0003
Family = 0x0004
FamORAssem = 0x0005
Public = 0x0006
field contract attributes.
Static = 0x0010
InitOnly = 0x0020
Literal = 0x0040
NotSerialized = 0x0080
SpecialName = 0x0200
interop and runtime attributes
PinvokeImpl = 0x2000
RTSpecialName = 0x0400
Reserved flags for runtime use only
ReservedMask = 0xd500
RTSpecialName = 0x0400
HasFieldMarshal = 0x1000
HasSecurity = 0x4000
HasDefault = 0x8000
HasFieldRVA = 0x0100
Member Name | Description |
---|---|
Assembly | Indicates that the field is accessible throughout the assembly. |
FamANDAssem | Indicates that the field is accessible only by sub-types in this assembly. |
Family | Indicates that the field is accessible only by type and sub-types. |
FamORAssem | Indicates that the field is accessible by sub-types anywhere, as well as throughout this assembly. |
FieldAccessMask | Specifies the access level of a given field. |
HasDefault | Indicates that the field has a default value. |
HasFieldMarshal | Indicates that the field has marshalling information. |
HasFieldRVA | Indicates that the field has a Relative Virtual Address (RVA). 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. |
HasSecurity | Indicates that the field has a security associate. |
InitOnly | Indicates that the field is initialized only, and cannot be written after initialization. |
Literal | Indicates that the field's value is a compile-time (static or early bound) constant. No set accessor. |
NotSerialized | Indicates that the field does not have to be serialized when the type is remoted. |
PinvokeImpl | Indicates that the field's implementation is forwarded through PInvoke. |
Private | Indicates that the field is accessible only by the parent type. |
PrivateScope | Indicates that the field cannot be referenced. |
Public | Indicates that the field is accessible by any member for whom this scope is visible. |
ReservedMask | [To be supplied.] |
RTSpecialName | Indicates that the NGWS runtime (metadata internal APIs) should check the name encoding. |
SpecialName | Indicates a special method, with the name describing how the method is special. |
Static | Indicates that the field represents the defined type, or else it is per-instance. |
Namespace: System.Reflection
Assembly: mscorlib.dll
[C#]
//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 (EnumInfo.IsDefined(typeof(FieldAttributes), Myattributesa)) Console.Write ("It has a {0} field attribute.", EnumInfo.ToString(typeof(FieldAttributes), Myattributesa)); 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 (EnumInfo.IsDefined(typeof(FieldAttributes), Myattributesb)) Console.Write ("It has a {0} field attribute.", EnumInfo.ToString(typeof(FieldAttributes), Myattributesb)); 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 (EnumInfo.IsDefined(typeof(FieldAttributes), Myattributesc)) Console.Write ("It has a {0} field attribute.", EnumInfo.ToString(typeof(FieldAttributes), Myattributesc)); 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.