FieldInfo.GetValue Method
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
When overridden in a derived class, returns the value of a field supported by a given object.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- obj
- Type: System.Object
The object whose field value will be returned.
Return Value
Type: System.ObjectAn object that contains the value of the field reflected by this instance.
| Exception | Condition |
|---|---|
| TargetException | The field is nonstatic and obj is null. |
| NotSupportedException | A field is marked literal, but the field does not have one of the accepted literal types. |
| FieldAccessException | The field is not accessible. |
| ArgumentException | The method is neither declared nor inherited by the class of obj. |
| MethodAccessException | The member is invoked late-bound through mechanisms such as Type.InvokeMember. |
In Windows Phone apps, only accessible fields can be retrieved using reflection.
If the field is static, obj is ignored. For nonstatic fields, obj should be an instance of a class that inherits or declares the field. Note that the return type of GetValue is Object. For example, if the field is of type Boolean, its value is boxed and returned as an instance of Object.
Version Notes
Windows Phone
GetValue throws an ArgumentNullException when null is passed as first parameter.The following example retrieves all the fields of the Test class and displays the field values. If a field value cannot be retrieved because of the field's access level, the exception is caught and a message is displayed. The internal fields (Friend fields in Visual Basic) are accessible in this example because the Example and Test classes are in the same assembly.
Note: |
|---|
To run this example, see Building examples that have static TextBlock controls for Windows Phone 8. |
using System; using System.Reflection; public class Test { public static string SA = "A public shared field."; internal static string SB = "A friend shared field."; protected static string SC = "A protected shared field."; private static string SD = "A private shared field."; public string A = "A public instance field."; internal string B = "A friend instance field."; protected string C = "A protected instance field."; private string D = "A private instance field."; } public class Example { public static void Demo(System.Windows.Controls.TextBlock outputBlock) { // Create an instance of Test, and get a Type object. Test myInstance = new Test(); Type t = typeof(Test); // Get the shared fields of Test, and display their values. This does not // require an instance of Test, so null is passed to GetValue. FieldInfo[] sharedFields = t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); foreach( FieldInfo f in sharedFields ) { try { outputBlock.Text += String.Format("The value of Shared field {0} is: {1}\n", f.Name, f.GetValue(null)); } catch { outputBlock.Text += String.Format("The value of Shared field {0} is not accessible.\n", f.Name); } } // Get the instance fields of Test, and display their values for the instance // created earlier. FieldInfo[] instanceFields = t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); foreach( FieldInfo f in instanceFields ) { try { outputBlock.Text += String.Format("The value of instance field {0} is: {1}\n", f.Name, f.GetValue(myInstance)); } catch { outputBlock.Text += String.Format("The value of instance field {0} is not accessible.\n", f.Name); } } } } /* This example produces the following output: The value of Shared field SA is: A public shared field. The value of Shared field SB is: A friend shared field. The value of Shared field SC is not accessible. The value of Shared field SD is not accessible. The value of instance field A is: A public instance field. The value of instance field B is: A friend instance field. The value of instance field C is not accessible. The value of instance field D is not accessible. */
Note: