Type.GetFields Method
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Returns all the public fields of the current Type.
Assembly: mscorlib (in mscorlib.dll)
The GetFields method does not return fields in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which fields are returned, because that order varies.
The following table shows what members of a base class are returned by the Get methods when reflecting on a type.
Member Type | Static | Non-Static |
|---|---|---|
Constructor | No | No |
Field | No | Yes. A field is always hide-by-name-and-signature. |
Event | Not applicable | The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below. |
Method | No | Yes. A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature. |
Nested Type | No | No |
Property | Not applicable | The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below. |
Notes:
Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. This is a binary comparison.
For reflection, properties and events are hide-by-name-and-signature. If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.
Custom attributes are not part of the common type system.
If the current Type represents a constructed generic type, this method returns the FieldInfo objects with the type parameters replaced by the appropriate type arguments.
If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the public fields of the class constraint.
The following example shows a use of the GetFields() method.
Note: |
|---|
To run this example, see Building examples that have static TextBlock controls for Windows Phone 8. |
using System; using System.Reflection; class Example { public static void Demo(System.Windows.Controls.TextBlock outputBlock) { Type myType = typeof(EventAttributes); outputBlock.Text = String.Format("Fields of {0}, flagging FieldAttributes.SpecialName:\n", myType.Name); foreach (FieldInfo fi in myType.GetFields()) { // Determine whether or not each field has a special name. if (fi.IsSpecialName) { outputBlock.Text += String.Format("{0} has the FieldAttributes.SpecialName attribute.\n", fi.Name); } else { outputBlock.Text += fi.Name + "\n"; } } } } /* This example produces output similar to the following: Fields of EventAttributes, flagging FieldAttributes.SpecialName: value__ has the FieldAttributes.SpecialName attribute. None SpecialName ReservedMask RTSpecialName */
Note: