
.NET 1.1+Reflecting Field Information
The third part of the Reflection tutorial looks at reflection of fields. Information about a type's fields can be obtained in FieldInfo objects, using System.Type class methods. FieldInfo objects include properties that describe a field's type and scope.
Fields
Fields are possibly the simplest members of a class so are a good starting point for an examination of the reflection of type members. Over the next few articles of this tutorial we'll expand the description of reflection of members to include properties, methods, constructors and other member types.
You can use reflection to obtain information about all of the fields in a class, including private, protected and internal fields that may otherwise not be available. In this article we will see how to obtain FieldInfo objects that are linked to fields and use the more common properties of this class to examine the field's type and scope. You can also use reflection to read the values from an object's fields and change those values, even if the field is private. This will be examined later in the tutorial.
To follow the code samples, create a new console application project. As we will be using types from the System.Reflection namespace, add the following using directive to your code:
To show the use of reflection to examine fields we need a class with a number of fields, each declared differently. We'll use the following code:
public class FieldTest
{
public string PublicField;
internal string InternalField;
protected string ProtectedField;
private string PrivateField;
public static string StaticField;
protected internal string ProtectedInternalField;
public readonly string ReadOnlyField;
private const string ConstantField;
}
Obtaining Information for a Public Field
If you wish to examine a public field of a type you must first create a Type instance, then you can use the GetField method to obtain a FieldInfo object. The simplest overloaded version of the GetField method has a single parameter that receives a string. The string should contain the name of the field to be examined.
In the following sample code we start by creating a Type object for the FieldTest class, then use GetField to get a FieldInfo instance for the PublicField field. Note that the case of the field must be correct. If you misspell the field name or use the incorrect mix of upper and lower case, the method returns null. The final line of the example outputs the name of the field, which is held in the Name property of the FieldInfo class.
Type type = typeof(FieldTest);
FieldInfo info = type.GetField("PublicField");
Console.WriteLine(info.Name);
// Outputs "PublicField"
12 February 2012