GetValue Method

FieldInfo.GetValue Method (Object)

 

When overridden in a derived class, returns the value of a field supported by a given object.

Namespace:   System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

public abstract object GetValue(
	object obj
)

Parameters

obj
Type: System.Object

The object whose field value will be returned.

Return Value

Type: System.Object

An object containing the value of the field reflected by this instance.

Exception Condition
TargetException
System_CAPS_noteNote

In the .NET for Windows Store apps or the Portable Class Library, catch Exception instead.

The field is non-static and obj is null.

NotSupportedException

A field is marked literal, but the field does not have one of the accepted literal types.

FieldAccessException
System_CAPS_noteNote

In the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, MemberAccessException, instead.

The caller does not have permission to access this field.

ArgumentException

The method is neither declared nor inherited by the class of obj.

If the field is static, obj is ignored. For non-static 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 holds a Boolean primitive value, an instance of Object with the appropriate Boolean value is returned. Before returning the value, GetValue checks to see if the user has access permission.

System_CAPS_noteNote

Access restrictions are ignored for fully trusted code. That is, private constructors, methods, fields, and properties can be accessed and invoked through reflection whenever the code is fully trusted.

System_CAPS_noteNote

Starting with the .NET Framework 2.0 Service Pack 1, this method can be used to access non-public members if the caller has been granted ReflectionPermission with the ReflectionPermissionFlag.RestrictedMemberAccess flag and if the grant set of the non-public members is restricted to the caller’s grant set, or a subset thereof. (See Security Considerations for Reflection.)

To use this functionality, your application should target the .NET Framework 3.5 or later.

The following example uses the GetValue method to retrieve the value of a static field. Note that the value of the obj argument is null.

using System;
using System.Reflection;

class Example
{
    public static String val = "test";

    public static void Main()
    {
        FieldInfo fld = typeof(Example).GetField("val");
        Console.WriteLine(fld.GetValue(null));
        val = "hi";
        Console.WriteLine(fld.GetValue(null));
    }
}
// The example displays the following output:
//     test
//     hi

The following example retrieves an array of FieldInfo objects that represents the fields of the FieldsClass type, and then calls the GetValue to display the value of each field for the fieldsInst object.

using System;
using System.Reflection;

public class FieldsClass
{
    public string fieldA;
    public string fieldB;

    public FieldsClass()
    {
        fieldA = "A public field";
        fieldB = "Another public field";
    }
}

public class Example
{
    public static void Main()
    {
        FieldsClass fieldsInst = new FieldsClass();
        // Get the type of FieldsClass.
        Type fieldsType = typeof(FieldsClass);

        // Get an array of FieldInfo objects.
        FieldInfo[] fields = fieldsType.GetFields(BindingFlags.Public 
            | BindingFlags.Instance);
        // Display the values of the fields.
        Console.WriteLine("Displaying the values of the fields of {0}:",
            fieldsType);
        for(int i = 0; i < fields.Length; i++)
        {
            Console.WriteLine("   {0}:\t'{1}'",
                fields[i].Name, fields[i].GetValue(fieldsInst));
        }
    }
}
// The example displays the following output:
//     Displaying the values of the fields of FieldsClass:
//        fieldA:      'A public field'
//        fieldB:      'Another public field'

ReflectionPermission

for accessing non-public fields when the grant set of the non-public members is restricted to the caller's grant set, or a subset thereof. Associated enumeration: ReflectionPermissionFlag.RestrictedMemberAccess

ReflectionPermission

for accessing non-public fields regardless of their grant set. Associated enumeration: ReflectionPermissionFlag.MemberAccess

ReflectionPermission

when invoked late-bound through mechanisms such as Type.InvokeMember. Associated enumeration: ReflectionPermissionFlag.MemberAccess.

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2016 Microsoft